Only 49 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help needed plz  RSS feed

 
Johnson David
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends ,

How many ways are there to stop a thread ?

what are they ?

thanx in advance

regards

johnson
 
Harald Kirsch
Ranch Hand
Posts: 37
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by johnson david:
Hi friends ,

How many ways are there to stop a thread ?

what are they ?

thanx in advance

regards

johnson


That depends on the question. How many ways are there to stop any Thread? None that you should use. How many ways are there to code a Thread such that it can be killed? It's only limited by your imagination. The most common way is by having a loop in the run() method that checks a condition. Once the condition is no longer met the loop will break and the method will exit so that the Thread will die.

A Thread is an Object. It's a special kind of Object though as it starts a new thread of execution. This is the basis for multithreading and allows you to make use of multiple processors or to continue processing instructions even while waiting on a resource.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the thread is looping while some condition is true you can make the condition false via a setter for example. (What Ken said)

If the thread is blocking on a wait(), you can interrupt() it.

If the thread is blocking on some other operation, like a db connect or a stream read, you may or may not be able to break that other operation. This is often a tough situation with no easy solution.

More?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But interrupt doesn't stop a thread does it?
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
But interrupt doesn't stop a thread does it?


No it doesn't -- but I think Stan was just elaborating the "setting the exit condition" example. Interrupting it will causes it to break out of wait() and hence, can check whether it should exit.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!