This discussion is archived
2 Replies Latest reply: Sep 19, 2012 7:05 PM by jtahlborn RSS

Parent thread wait for/kill children

910971 Newbie
Currently Being Moderated
I've not done much with concurrent programming in Java. After reviewing both the Java concurrency tutorial and also the concurrency API documentation, I'm still unclear how to implement my specific use case. I'd appreciate any recommendations regarding which Java classes to use or other resources I can investigate to get a better understanding. Consider the following code:
Collection<Node> nodes = getNodes();
Node firstReplier;
for (Node n : nodes) {
    if (n.isAvailable()) {
        firstReplier = n;
        break;
    }
}
//Additional processing with firstReplier if != null...
The parent gets a collection of nodes and then iterates through them, asking each one if it is available. The above algorithm is sequential; it will ask each node in the list if it is available, wait for a reply, and then go on to the next node if it is not. I have two questions:
1) I'd like to spawn a new thread to wait for a reply from each individual node concurrently. What is the best way to achieve this?
2) As soon as one node replies (firstReplier), I'd like the parent to continue processing with that node, and also to instruct all other threads waiting for replies from other nodes to terminate. What is the best way to achieve this?
  • 1. Re: Parent thread wait for/kill children
    DrClap Expert
    Currently Being Moderated
    The usual way to tell a thread to stop doing what it is doing is via a boolean flag in the thread.

    Initialize it to false. Then the thread will periodically check it and terminate if it has been set to true. Obviously, then, if you want the thread to terminate then you set the flag to true.

    This implies you have to write your threads so that they do check the flag regularly while they're doing their work.

    Also note that once you get a reply from one node, by the time you notify all of the other threads to knock it off, some of them may already have got replies from their nodes. If you want those other threads to not act upon the reply, then you need a shared object which only allows one thread to proceed. A CountdownLatch would be a good tool for this, I think.

    It's also possible (and quite likely) that you could write a customized ExecutorService which took care of all those details for you. I haven't had all that much experience with Executors so I can't envision all of the details, but that's what I would try if I could. My point of view is that low-level threading with wait() and lock() still exists, but you should really try to do things in java.util.concurrent as much as possible for real-life programming.
  • 2. Re: Parent thread wait for/kill children
    jtahlborn Expert
    Currently Being Moderated
    crottyan wrote:
    1) I'd like to spawn a new thread to wait for a reply from each individual node concurrently. What is the best way to achieve this?
    2) As soon as one node replies (firstReplier), I'd like the parent to continue processing with that node, and also to instruct all other threads waiting for replies from other nodes to terminate. What is the best way to achieve this?
    You want to use an ExecutorCompletion service. in fact, the class javadoc for ExecutorCompletionService has exactly this example (second example).

    http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorCompletionService.html

    Edited by: jtahlborn on Sep 19, 2012 10:04 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points