• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question thread  RSS feed

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i stop the Thread?
I have heard that Thread.stop() is unsafe.
What is the other way to stop the thread?
Which thread can we stop ?
If we write Thread.currentthread.stop(), which thread will stop?

Please expalin with example...
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh: How can i stop the Thread?

You can not *forcibly* stop a thread. You can only signal it to stop.
What I mean is, you can use a volatile boolean flag that the running thread checks before every iteration to see whether it should continue. If the boolean is false, it must quit. Something like:



In the above code "shouldRun" is a volatile boolean member variable.

Additionally, you can try interrupting the thread. If the thread responds to interrupts, it will stop by throwing an InterruptedException.

All this and more in this article

Dinesh:
I have heard that Thread.stop() is unsafe.

Yeah. See the above link for the reason.
Dinesh:
Which thread can we stop ?

Any thread that you can get a handle to.

Dinesh:
If we write Thread.currentthread.stop(), which thread will stop?

The thread that has called this line of code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!