A thread exit is best implemented by itself, always checking a state in the running state, if the state is true, running all the time, and if the state variable is changed by the outside world, then the thread stops running.
1.sleep () method
Suspends execution of the currently executing thread for a specified period of time, but does not release the lock flag. It is not recommended.
Sleep () causes the current thread to enter a blocking state that will not be executed for a specified time period.
2.wait () method
Causes the current thread to wait before another thread calls the object's notify or Notifyall method. The thread releases the "lock flag" that it occupies, allowing other threads to seize the lock.
The current thread must have the current object lock. If the current thread is not the owner of this lock, a Illegalmonitorstateexception exception is thrown.
The waiting thread that wakes the current object lock uses the Notify or Notifyall method, and must also have the same object lock, or the illegalmonitorstateexception exception is thrown.
Waite () and notify () must be called in the synchronized function or synchronized block. If the call is made in the non-synchronized function or non-synchronized block, the illegalmonitorstateexception exception will occur at run time, although it can be compiled.
3.yield method
Pauses the currently executing thread object.
Yield () simply returns the current thread back to the executable state, so the thread executing yield () is likely to be executed immediately after entering the executable state.
Yield () only causes threads with the same priority or higher priority to have an opportunity to execute.
4.join method
Waits for the thread to terminate.
Wait for the thread that called the Join method to end, and then continue execution. such as: T.join ();//mainly used to wait for the T thread to run the end, without this sentence, main will be executed, resulting in unpredictable results.
Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5
"Java Framework source code download"
Analysis of Sleep (), wait (), yield (), join () methods in Java