[concurrency-interest] Using concurrent to write a load balancer

Robert Bowen syg6 at yahoo.com
Mon Sep 18 08:03:27 EDT 2006


Hi Kasper, thanks for the response.

Yea, I was meaning to look at invokeAny(). My only problem with ExecutiveService is, I still need a mechanism that allows me to launch every thread / task in my pool at the same time, instead of looping through each thread, one at a time, and calling its start() method, and then having it run.

I thought using CyclicBarrier would be the answer but I'm having issues with that as well -- A program that uses CyclicBarrier does the following:

1. Create the barrier, say with 5 threads.
2. Loops through all of the threads calling each thread's start() method.
3. When all of the threads are started the barrier is 'breached' and they all execute.

Fine. But what I want to do is start all the threads and have the barrier wait for some *other* event, in my case, an incoming request. And upon receiving it, launch all of the threads, grab the result from the 1st one, cancel the rest, and go back to waiting. 

It would seem I can't do this with CyclicBarrier, because it doesn't wait for external events but rather other threads to be started. One they are all started it triggers. It would seem a CountDownLatch would make more sense but they are not reusable! 

In summary, I'd love to use the ExecutiveService.invokeAny() but I still need a way to continually (with each request) launch all of my threads at the same time.

Can I do this with ExecutiveService?

Thanks again,
Bob

----- Original Message ----
From: Kasper Nielsen <kav at it.edu>
To: syg6 <syg6 at yahoo.com>
Cc: concurrency-interest at cs.oswego.edu
Sent: Monday, September 18, 2006 1:23:44 PM
Subject: Re: [concurrency-interest] Using concurrent to write a load balancer

syg6 wrote:
> Hello all.
> 
> I am trying to write a load balancer using the new Java 1.5 concurrent
> classes. I am currently trudging through all of the examples, trying to see
> what I might use. I wanted to bounce my idea off the group.
> 
> I will have a more-or-less fixed pool of a small number of threads (it might
> grow or shrink every now an then but not often). Each thread will have the
> same Task associated with it, a task that sends out a 'ping' (using
> java.net's isReachable, which is another can of worms, but anyway ...)
> 
> So every time a request comes in I'd like to use a CyclicBarrier (I think)
> to launch all of the threads and when the first thread answers (with the ip
> of the machine pinged), cancel all of the other Tasks, reset and wait for
> the next request.
> 
> I *think* the best way to do this is with a CyclicBarrier, because I need a
> reusable way to launch all of my threads/tasks at once. CountDownLatch isn't
> reusable and it I were to use a ExecutorCompletionService I'd have to call
> the submit() method for each Task, one after the other, which means the
> first Task would probably almost always be the first to respond.
> 
Hi Bob,

I think you might want to have a look at
the invokeAny method of ExecutorService
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#invokeAny(java.util.Collection)

Should be much simpler then using a CyclicBarrier.

- Kasper




-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060918/fbe88b06/attachment.html 


More information about the Concurrency-interest mailing list