Thread collaboration Methods: sleep, yield, wait, join, and sleepyield
Sleep)
- When the t Thread calls Thread. sleep (), the Thread t will sleep for a specified time.
- Thread. sleep () and Thread. currentThread (). sleep () have the same effect.
- The sleep method does not release the synchronization lock.
- Sleep does not affect other threads.
Yield (concession)
- When Thread. yield () is called in the t Thread, the t Thread will discard the cpu scheduling (usually the current scheduling)
- Yield allows the current thread to give in to other threads for execution
Wait (Lock wait)
- When the t thread calls obj. when wait (), the t thread will enter the waiting state and discard the obj object lock until other threads call obj. Y ()/notifyAll () method to wake up thread t
- The wait method releases the synchronization lock.
- The thread can only call the wait method in one synchronization block.
- The wait method allows two threads to run alternately.
Join)
- When the main thread calls the t. join () thread, the main thread will wait until the t thread is fully executed.
- Join allows two threads to be executed in serial mode