Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ram Para
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ?
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic