[concurrency-interest] Using concurrent to write a load balancer
Stefan.Skoglund at it-huset.se
Mon Sep 18 08:05:27 EDT 2006
CyclicBarrier has all or nothing guarantees. The idea is to let exactly, in this case 5, threads fall through. It will never let more or less than 5 threads fall through at once. This is a tricky thing to implement, and the concurrent guys has done it for you. Still, it is a rather tricky to use.
It seems that this isn't the thing you are looking for though. It sounds like you need a powerful thread pool. Take a look at the ExecutorService, also in concurrent.
Från: concurrency-interest-bounces at cs.oswego.edu genom syg6
Skickat: må 2006-09-18 12:40
Till: concurrency-interest at cs.oswego.edu
Ämne: [concurrency-interest] Using concurrent to write a load balancer
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.
The only possible problem I see with CyclicBarrier is, what happens when,
say, a request comes in, I launch my 5 (for example) threads, one responds
but before the other 4 can respond I get another request? Then I'll have to
wait until they all respond to re-launch. Ideally I'd like to re-launch
immediately with the 1 thread I have available, but I don't know if this is
Does this sound doable? Anyone have any code that resembles this?
ps. I posted on this last week on the java.sun.com forums, but decided to
move over to Nabble ... Here's the
http://forum.java.sun.com/thread.jspa?threadID=768022&tstart=0 link :
View this message in context: http://www.nabble.com/Using-concurrent-to-write-a-load-balancer-tf2290433.html#a6361357
Sent from the JSR166 Concurrency mailing list archive at Nabble.com.
Concurrency-interest mailing list
Concurrency-interest at altair.cs.oswego.edu
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Concurrency-interest