Javamex

Java discussion forum to accompany the Javamex web site.

Hi,

Found the article on http://www.javamex.com/tutorials/threads/CountDownLatch.shtml, very explanatory....

That way multiple threads can be forked off, and the forker (parent thread) made to wait for them to finnish before procedding.

A query, in my program, a parent threads spwans off multiple child threads, which all do an 'interesting job'.
The 'interesting job' per se, is a JMS request sent to a sub-system, which can return a status like SUCCESS, FAILURE or TIME-OUT
However,
The parent thread should not proceed unless the forked off child threads complete.

So using the CountDownLatch I guess the parent thread can block (pause), for the child threads to finnish.
...but the catch is;
...The parent thread should continue its further, normal job (if and only if the child threads complete SUCCESSFULLY)
...If not, it, the parent thread too, should abort and terminate, returning back to caller.

Anyway in concurrency f/w that can be used to achieve this?

Currently I am achieving this is using a shared memory area (an array holding the statuses of the child threads) which the parent thread keeps checking, traversing all the array elements, in a while loop, and counting the 'SUCCESSES'.

But I feel the while loop is proving very expensive, on the performance aspect, as it seems the parent thread in which it is executing hogs onto the cpu.

thx
~g1

Views: 141

Reply to This

Replies to This Discussion

You certainly shouldn't sit polling-- as you say, you just end up burning CPU...

There are at least a couple of options depending on your exact needs. If you can wrap a Callable around your queries, then you can use the Executor.invokeAll() method. The essential idea is that you create a list of Callables representing your requests, which are allowed to return results/throw exceptions. Then you pass that list to invokeAll(), which returns you a list of Future objects. Then you cycle through that list of Future objects, calling get() on each in turn. If any of the requests had errors, then the corresponding Future.get() call will throw an exception, so you know there was a failure.

You could potentially also use a CyclicBarrier, or even just a BlockingQueue (which the threads making the requeswts add their results to, and then the controller thread sits repeatedly calling take() to get the results off the queue until it has received the expected number, then can check if they're all ob jects representing SUCCESS).

I'm hopefully going to get some examples of invokeAll() and CyclicBarrier up on the site soon.
Hi Neil,

I think CountDownLatch nor BlockingQueue can work for me.

To elaborate/ re-phrase my scenario;

The parent thread does not fork off new threads explicitly, but rather only does a sendJMSMessage(). The receipent subsystem does the 'interesting job' and returns a status ... also over JMS.

The JMS-listener in my code, gets activated (a new thread is launched) on receipt of the status message.

My requirement is the parent thread (that sent the several JMS requests) has to pause (wait) until all responses are received/ processed. (if 5 JMS requests were sent the parent needs to wait for all 5 respones = 5 intances of the JMS listener to finish processing.);

and so CountDownLatch cannot be used.

thx
g1
So just let me make sure I understand-- because I think that's the perfect case for CountDownLatch:

- your thread firing off the jobs constructs a CountDownLatch with the number of messages it's going to fire, fires off the messages, and awaits the latch;
- in your JMS-listener code, you count down the latch.

Remember the await() method ONLY returns once the latch has been counted down the number of expected times-- i.e. for the number of expected messages.

Sorry, I confess I'm not too familiar with the specifics of JMS, but inprinciple that's how it should work.

Jeevan said:
Hi Neil,
I think CountDownLatch nor BlockingQueue can work for me.
To elaborate/ re-phrase my scenario;

The parent thread does not fork off new threads explicitly, but rather only does a sendJMSMessage(). The receipent subsystem does the 'interesting job' and returns a status ... also over JMS.

The JMS-listener in my code, gets activated (a new thread is launched) on receipt of the status message.

My requirement is the parent thread (that sent the several JMS requests) has to pause (wait) until all responses are received/ processed. (if 5 JMS requests were sent the parent needs to wait for all 5 respones = 5 intances of the JMS listener to finish processing.);

and so CountDownLatch cannot be used.

thx
g1

Reply to Discussion

RSS

© 2024   Created by Neil Coffey.   Powered by

Badges  |  Report an Issue  |  Terms of Service