The sleep () method and yield () method are both static methods of the thread class, which will cause the current running thread to discard the CPU and run the job.
To another thread. The difference between the two is:
1. The sleep () method will give other threads a chance to run without considering the priority of other threads. Therefore, it will give a chance to run at a lower priority;
The yield () method only gives the thread with the same or higher priority a chance to run.
2. When the thread executes the sleep (long millis) method, it will go to the blocking state. The parameter millis specifies the sleep time. When the thread executes yield ()
After the method is used, it is changed to the ready state.
3. The sleep () method declaration throws an interruptedexception exception, while the yield () method does not declare that it throws any exception.
4. The sleep () method has better portability than the yield () method. The yield () method cannot be used to improve the concurrent performance of the program. For most
For programmers, the only purpose of the yield () method is to artificially improve the concurrent performance of the program during the test to help discover hidden errors.