The difference between the sleep () method and the yield () method is as follows:
1 consider the different priority of threads
The sleep () method gives other threads the opportunity to run without considering the priority of the thread, that is, it gives the low-priority thread a chance to run. The yield () method only gives the opportunity to run a thread of the same priority or higher priority.
2 The timing of the re-execution is different
The sleep () method blocks the current thread and will not be executed during the sleep period. The yield () method simply returns the current thread back to the executable state, and the current thread may be executed immediately after entering the executable state.
3 Whether throwing exceptions is different
The sleep () method declaration throws interruptedexception, and the yield () method declaration does not throw any exceptions.
4 Portability is different
The sleep () method is more portable than the yield () method, which is related to the operating system.
Resources
"Java Programmer interview written book" P150
The difference between the Java multithreading Sleep () method and the yield () method