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

David Holmes dcholmes at optusnet.com.au
Tue Sep 19 00:49:34 EDT 2006


This approach might be viable for you but it doesn't deal with two things
you initially indicated were an issue:

a) there will be a skew toward the first Ping always being the first to
respond because of the time taken to add all the Ping tasks to the
executor - especially in light of how invokeAny works. Nowhere near as bad
as starting new threads but still might be an issue. Of course there is no
instantaneous release mechanism - even using a CyclicBarrier requires the
released threads to serialize through the internal lock, one at a time.

b) No cancellation. But as I've said before the ping isn't cancellable - so
you really have no choice here but to live with that and just use more

  -----Original Message-----
  From: concurrency-interest-bounces at cs.oswego.edu
[mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Robert Bowen
  Sent: Tuesday, 19 September 2006 1:56 AM
  To: Kasper Nielsen
  Cc: concurrency-interest at cs.oswego.edu
  Subject: Re: [concurrency-interest] Using concurrent to write a load

  Well I have something up and running using ExecutorService. Basically it's
like this:

  class Ping implements Callable<String> {

      public String call() throws Exception {
         InetAddress iNetAddress = InetAddress.getByName(address);
         if (iNetAddress.isReachable(3000)) return address;
         else return null;

  public class ThreadPoolExecutorTest {

      public static void main(String[] args) {
          ThreadPoolExecutor tpe = new ThreadPoolExecutor(...);

          ArrayList<Callable> tasks = new ArrayList<Callable>();
          Ping ping = new Ping("");
          ping = new Ping("");

          while (true) {
              String result = (String)tpe.invokeAny(tasks);

  It's just a demo but it seems to work very well, many thanks for all of
the tips! Working with threads is confusing to start with in Java and with
this new API it's *much* easier, but even so there's a bunch of different
classes that have similar functionality, it's a question of navigating
through all the docs and (of course) speaking to people more clever than
yourself on the forums.

  My only other question is how to cancel the other tasks that finish after
the first one, so they are available again for the next request? I know I
can cancel tasks using Future.cancel() but if I use a FutureTask instead of
a Callable, then I can't use invokeAny() ...

  Anyway, I'm further along than I was with your help!


  ----- 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

  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
  > 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
  > grow or shrink every now an then but not often). Each thread will have
  > 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
  > to launch all of the threads and when the first thread answers (with the
  > of the machine pinged), cancel all of the other Tasks, reset and wait
  > 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
  > reusable and it I were to use a ExecutorCompletionService I'd have to
  > 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


  Should be much simpler then using a CyclicBarrier.

  - Kasper
  Concurrency-interest mailing list
  Concurrency-interest at altair.cs.oswego.edu

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060919/39d5253c/attachment.html 

More information about the Concurrency-interest mailing list