How can you stop a thread?

1 post / 0 new
heartin
How can you stop a thread?

Previously there was a method called stop() which is now deprecated.

Currently, a thread should signal another thread that it should stop executing by calling the interrupt() method for that thread. This doesn’t stop the thread, but sets a flag in the thread. This flag must be checked in the run() method to have any effect and the thread should then terminate itself.

The isInherited() method that the thread class defines returns true if the interrupted flag has been set. This method does not reset the flag, but another method interrupted() tests the flag and resets it if it was set. This is a static method in thread class that tests whether the currently executing thread has been interrupted, and if it has, it clears the interrupted flag in the current thread object and returns true.

When an InterruptedException is thrown, the flag that registers the interrupt is cleared, so a subsequent call to isInterrupted() or interrupted() returns false.

 

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!