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

Kasper Nielsen kav at it.edu
Mon Sep 18 07:23:44 EDT 2006


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


More information about the Concurrency-interest mailing list