Wake up, wait, and kill one / many threads

Hello all.

I am trying to do something that I originally wanted to do with multicasting, but in the end could not.

Essentially I want to create a bunch of threads, one for each machine I want to 'ping' (not the actual ping, just some kind of 'are you alive?'). These threads will just sit there and wait upon creation.

Then when a request comes in I want to:

1. wake them all up so they all send a 'ping' to their respective server.

2. retrieve the ip address (or name) of the first thread that replies

3. kill the rest of the threads (if they haven't already died)

4. go back to waiting

I've been surfing Google, Java tutorial and these forums and can't seem to put it all together.

Many thanks for any help!

Bob

[790 byte] By [syg6a] at [2007-10-3 4:53:42]
# 1
what specifically is that's confusing you? May be you need to go through some tutorials
kilyasa at 2007-7-14 22:58:34 > top of Java-index,Core,Core APIs...
# 2
Is that an infinite loop scenario?If it is, some of synchronizers objects from java.util.concurrent package should be handy for controlling states of the multiple threads. Semaphore, CountDownLatch, CyclicBarrier, ...See the API doc for their detail and usages.
hiwaa at 2007-7-14 22:58:34 > top of Java-index,Core,Core APIs...
# 3

You can use a CountDownLatch to have them all wait and then signal they should continue with the "ping".

Waiting for the first answer is a bit trickier but you could take a look at Futures/FutureTask - the main thread does a get() to wait for the answer and the other threads try to set() but with things arranged so that only one set is possible.

I don't see the point in terminating them if you are going to repeat the exercise.

I suggest reading up on concurrent programming in Java - see "Java Concurrency in Practice" by Goetz et al.

Message was edited by:

davidholmes

davidholmesa at 2007-7-14 22:58:34 > top of Java-index,Core,Core APIs...
# 4

Hello again, thanks for all of the responses.

I feel like a bit of a schmuck, I had no idea J2SE 5.0 had all of this new concurrency stuff. All of these things -- Semaphores, CountDownLatch, etc. -- are new to me. I've been trapped in the web / MVC world too long. I've used threads in the past but only for simple stuff, always trying to avoid all the messy stuff like synchronized blocks, wait(), notify(), etc.

So I am boning up on all this new stuff now, thanks.

As for you comment, 'I don't see the point in terminating them if you are going to repeat the exercise.', you're right, I didn't mean kill the threads that didn't reply first, I meant ignore them.

Basically I have a situation where I'll have something like 60 requests per minute on average. For each request I want to wake up all the threads, send out a ping and send the request to the ip address of the first thread that replies.

The problem of course is that during that one request I may have another, and another, since a ping could take anywhere from less than a second to 5 seconds to timeout. So, for example, If another request comes in while 3 out of the total of 5 threads are still pinging, only 2 will be woken up to send out another ping, and so on.

It's complicated and it would be a lot easier with a simple multicast message using JGroups or a simple Java multicasting. But alas since these servers that I'll be pinging aren't ours, I can neither install a small multicast listener program on them or even be sure multicasting (port 124.0.0.1) is enabled.

So that's the deal. I'm going to keep reading. Thanks for all of your help!

Bob

syg6a at 2007-7-14 22:58:34 > top of Java-index,Core,Core APIs...