aspose file tools*
The moose likes Threads and Synchronization and the fly likes How I can make sure that a particular thread will run in the end. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How I can make sure that a particular thread will run in the end." Watch "How I can make sure that a particular thread will run in the end." New topic
Author

How I can make sure that a particular thread will run in the end.

Ram Para
Ranch Hand

Joined: Jul 09, 2008
Posts: 50
I have 10 threads. 9 have same implelentation and 10th has different implementation.
I need to invoke all of them.
But my problem is how I can make sure that 10th thread will run only after when other 9 are done ?

Priority is not working.

Thanks in advance


Ram Parashar
http://doinfinite.com
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

Without looking at your code hard to give a solution. Can you join the 10th thread to the last thread before that (means you may join the threads manually in the order you need them to run) ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
A possible approach

Keep a member variable numberOfThreadsCompleted in some shared object SO. Spawn the nine threads passing a reference to this SO.
Make the main thread wait as follows



In each of the 9 child threads,at the end of the run() method ,increment the so.numberOfThreadsCompleted in a synchronized method/block,and call so.notifyAll().

In the child threads


The main thread would then exit only when all the 9 child threads are done. Spawn the 10th thread at the end of the main method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Ajay Saxena wrote:A possible approach

Keep a member variable numberOfThreadsCompleted in some shared object SO. Spawn the nine threads passing a reference to this SO.
Make the main thread wait as follows



In each of the 9 child threads,at the end of the run() method ,increment the so.numberOfThreadsCompleted in a synchronized method/block,and call so.notifyAll().

In the child threads


The main thread would then exit only when all the 9 child threads are done. Spawn the 10th thread at the end of the main method.


This exact functionality is already available -- see the java.util.concurrent.CountDownLatch class. Using this class, the 10th thread simply calls the await() method, while the other 9 calls the countdown() method (upon completion).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
This exact functionality is already available -- see the java.util.concurrent.CountDownLatch class. Using this class, the 10th thread simply calls the await() method, while the other 9 calls the countdown() method (upon completion).


That would be good..You made me feel prehistoric Henry Looks like I really need to get my hands dirty with the java.util.concurrent classes... On a +ve note,getting down to the basics helps get the behind the scenes mechanics clear. That said,I again endorse the proposal for invoking the CountDownLatch APIs in this problem.
Faraz Syed
Greenhorn

Joined: Feb 12, 2009
Posts: 10
Java 1.5's CountDownLatch notwithstanding, I have a couple of questions about Ajay's solution:

Couldn't Thread.join() be used instead of the wait()-notifyAll() approach? I mean, the 10th thread would attempt to join on each of the preceding 9 threads.

Wouldn't that accomplish the desired result?

Lastly, are there any reasons to prefer the wait()-notifyAll() approach over trying to join() on each of the 9 threads?

I am a newbie when it comes to threads, and I have been thinking of this particular post for the last days.

Thanks!

Regards,
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Yes, calling join() on the previous nine threads would work just fine. It's probably what I would do. The only reason I can think of not to do that is if you don't already have direct access to to a collection or array of the thread objects, or if they come from a thread pool. For example, if you're using some sort of ExecutorService to run the tasks, you don't get direct access to the threads, and you shouldn't necessarily expect the threads to terminate, anyway - they may just return to a pool and await new tasks. So calling join() on the threads won't work well here. But it works fine if you're creating Threads directly, with new Thread(), and keep them in a collection or array.

Conversely, the CountDownLatch requires you to put code at the end of the tasks the other 9 threads are using, to ensure the countDown() method is guaranteed to be called exactly once. Not difficult, unless that code is outside your control.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How I can make sure that a particular thread will run in the end.