Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping threads  RSS feed

 
mandeepsangwan kumar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the problem is that i have 4 threads running and one of them is suppose to get the final result , now when one of them have achieve the result then how can i stop the other threads or tell other threads to stop and the other threads can conclude some work before stopping .

Thanks in advance
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with the threads having a loop dependent on a boolean which any thread can set false?
 
Henry Wong
author
Sheriff
Posts: 22817
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At minimum, you will have to do it cooperatively. Have the threads communicate with each other. For example, set some sort of done flag, to state that the task is done, and the other threads should just clean up after itself and finish. These other threads should never change anything, without checking this flag first (and do so in a thread safe manner).

Also, of course, the other threads will need to check the flag from time to time, and will be unable to clean up and finish up immediately, so your algorithm should not depending on it.

You can make the clean up of the other threads a bit more aggressive via the use of condition variables, interruption, or even closing resources -- however, I tend to recommend against that, as it it may make a mess of things (and harder to clean up).

Henry
 
mandeepsangwan kumar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well thank you
yes i got the answer .......
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Version 1 of Java had all sorts of methods in java.lang.Thread for controlling Threads.

Methods like "stop", "suspend" "resume" - but it was soon realized that actually using these was very dangerous because they could leave the whole system in a damaged state. Read the JavaDocs for comments on such methods.

Bill
 
Stevens Miller
Bartender
Posts: 1443
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stopping threads by having them check a boolean from time to time is a proven, simple technique that puts you in complete control of how a thread shuts down. I only add my approval of this methods because, when I first learned of it, I thought, "That seems awfully rudimentary, and Java comes with so many fine, high-level abstractions, that there just has to be a more sophisticated way to do it." But, there isn't (well, there are but if you don't them, well... then you don't need them.

My question about your situation, however, is this: how does one thread get the final result while the others are still running? If the others are helping compute that result, it's possible that they will know when they have done their part and can simply exit immediately afterwards. If so, then your final thread need not do any signaling (although, most likely, that final thread will need to be blocked or otherwise waiting on signals from those other threads, so it knows they have finished with whatever it is that they are doing).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!