wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Threads Completed Notification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads Completed Notification" Watch "Threads Completed Notification" New topic
Author

Threads Completed Notification

Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
Dear Experts,

With threads, if there are 1 writer and many readers, we can wait for the writer to finish his job and notify the waiting readers Threads that are in "wait()" mode.

Before i ask the question, a little background on this code:

1) The code has been written to allow a dynamic creation of threads depending on the workload
(maybe even 250 threads at the same time)

2) There is a "List" that contains the work to be done. In this case a list of IP adresses to scan.
each thread, checked the (synchronized) list and test one IP at the time, until the list is empty.

3) I Cannot check the list if is empty to know if they are finished, because some threads might have already
gotten their IP to ping, but the reply might come back after few seconds, therefore I had to rely on other methods

4) I've tried to make it work with "Wait" and "Notify" but it seems that in some cases the notify comes after the
Wait has been initialized, therefore the program is waiting forever for a notify that will never come.

Give the above, and looking at the code below, is there a better way else than the "while ( ..:" statement to look
or get notified of thread completition? I think the while is too much of an overhead to keep looping. In the "while"
statement, i'm counting for how many threads are finished and if the match the initial number, it means they are all
back...

The goal is to wait for all thread to finish, before print out the number of IPs found...



Thank you very much!
Dave


SCJP, SCBCD
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

If I get your meaning, you have one Thread which calls startWork which starts a bunch of parallel tasks (in new Threads), and you want the startWork method to wait (block) until all the tasks are completed.

There are a couple of tools you can use for this. One is to simply loop through the threads and join them:


Or there abouts. No waits or notifies.

Another uses some of the functionality in the java.util.concurrent package. You could create a CountDownLatch witch counts down from the number of Threads you created, and the startWork method would wait for the count down to complete:



But this requires Java 1.5+ and requires a change to the Network class which injects the counting/tracking mechanism into the work being done, which may not be what you want.

By the way, you have:


What on earth is that inner loop for? It doesn't do anything except add complexity to the code. Hopefully at runtime it would be optimized and unrolled so it has no effect whatsoever, but from a code perspective it can just lead to confusion about what you want, and maybe to bugs later on. Just do


Steve
Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
Hi Steve!

Thank you for taking the time to reply...

Yes... i forgot the inner loop when i was playing around with "synchronize" and "wait" and forgot to clean it up...

What you suggest (the countown) is what I'm doing now... i populate the array with all the Threads names and in the while loop i keep checking until the thread count of threads with status "Terminated" reach the same count of the started threads...

I cannot use the "Join" because all threads need to work in parallel and do their work at the same time (since it's a network test with a 5 sec. timeout, working togheter, they are finished in about 7 seconds from the start)...

...I will look in the java.util.concurrent class... to see if there is something interesting in there

Thanks!!
Dave
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Just thought of another way to do this, which would probably be easier to generalize and be more extensible.

Have a call back mechanism, where the Network class gets a reference to some Object and tells it when it is started and completed. You could then make this work to keep track of the running tasks and to add more / different functionality via extension later on.

Here is a rough outline. Start with an interface which defines 'callback' methods which indicate when a task is beginning and ending:


You could then have your class which has the startWork method implement the interface and perform the counting for you:


This way, the counting details stick in the class which does the counting, and you can add/change before/after work behavior by adding listeners - either directly to the Network object or with some modification, to the caller class. Be careful with this setup as shown, though. The work in the listeners is done in the same thread as the 'task' being worked on, so keep the 'listener' code short, simple, and preferably exception-free.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Davide Crudo wrote:Hi Steve!

Thank you for taking the time to reply...

Yes... i forgot the inner loop when i was playing around with "synchronize" and "wait" and forgot to clean it up...

What you suggest (the countown) is what I'm doing now... i populate the array with all the Threads names and in the while loop i keep checking until the thread count of threads with status "Terminated" reach the same count of the started threads...


It is the same concept, but I think the implementation using CountDownLatch will be simpler. It prevents the need for a while loop which consumes processor time, and will probably respond quicker than the 200ms sleep you put in your while loop.


I cannot use the "Join" because all threads need to work in parallel and do their work at the same time (since it's a network test with a 5 sec. timeout, working togheter, they are finished in about 7 seconds from the start)...


I don't know if I understand this concern. All the Threads work in parallel EXCEPT the one that calls startWork, which is waiting in a loop until all the other Threads reach the Terminated state. Thread.join() will wait until the particular thread dies - ie reached the Terminated state - so when you call it (in startWork) on each one of the worker Threads it does the same thing as your while loop, without having to call a method multiple times on the same Thread, and probably with a better responsiveness than the 200ms sleep you put into your loop.

If your concern is that Thread3 may complete before Thread2, well that isn't a problem. What would happen is that the startWork Thread would wait until Thread2 completes, when it does it will wait for Thread3 to complete, which will already be true so it would immediately return and continue to check Thread4.


...I will look in the java.util.concurrent class... to see if there is something interesting in there

Thanks!!
Dave


No problem. The java.util.concurrent package has a lot of goodies which makes Threading easier. If you have Java 1.5+ then you might consider using a ThreadPoolExecutor and Future objects to manage your tasks.
Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
Hi Steve!

A lot of nice and interesting topics! i will go thru them...it might take a while ;)...

In the mean time, I got your point about the Join... And I didn't know the JOIN could be used on multiple threads!

In fact... i've tried the join...and it worked beautifully!

This is exactly what i was looking for, because I felt the loop for checking the threads was too much of an
overhead and not really a nice design...

Thanks again for the great help!

Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Completed Notification
 
Similar Threads
Variable Thread Number
Last task not executed by thread from thread pool
notify()
Food for thought question (Threads related)
Multi-threads access singleton