Thread Sleep
If you need to suspend the currently executing thread for a period of time and enter a blocking state, you can do so by calling the static sleep method of the thread class. Two overloaded forms of the Sleep method:
1.static void sleep (Long Millis): lets the currently executing thread pause Millis milliseconds and enter a blocking state, which is affected by the accuracy and accuracy of the system timer and the thread scheduler.
2.static void sleep (Long millis,int Nanos): Lets the currently executing thread pause Millis milliseconds and enter a blocking state that is marketed by the accuracy and accuracy of the system timer and the thread scheduler.
The sleep method is often used to suspend execution of a program when the thread calls the sleeping method into the blocking state, and the threads do not get the chance to execute during their nap time, even if there are no other executable threads in the system, and the process is not executed.
Thread Concessions
The yield method is a somewhat similar method to the Sleep method, and it is also a static method provided by the thread class that allows the currently executing thread to pause, but it does not block the thread, it simply moves the thread into a ready state. Yield just let the current thread pause to have the system's thread scheduler reschedule once, and it is entirely possible that the thread scheduler will dispatch it again after a thread has called the yield method paused.
In fact, after a thread has called the yield method paused, only the priority is the same as the current thread, or a thread with a ready state that has a higher priority than the current thread will be given an opportunity to execute.
Package com.hehe.thread;
public class Yield extends thread{public
Yield (String name) {
super (name);
}
Define the Run method as the thread executor public
void run (
int i = 0; I <50; i++) {
System.out.println (getName () + "" + i); C8/>if (i==20) {
Thread.yield ();
}}} public static void Main (string[] args) throws exception{
//Starts two concurrent threads
Yield yieldthread1=new Yield ("advanced");
Yieldthread1.setpriority (thread.max_priority);
Yieldthread1.start ();
Yield yieldthread2=new Yield ("low Level");
Yieldthread2.setpriority (thread.min_priority);
Yieldthread2.start ();
}
The difference between sleep and yield:
The 1.sleep method pauses the current thread, giving other threads an opportunity to ignore the priority of the other thread, but the yield method only gives the same priority, or higher priority, to the thread execution opportunity.
The 2.sleep method turns the thread into a blocking state, knowing that the blocked time will go into the ready state, and yield does not turn the thread into a blocking state, it simply forces the current thread into a ready state. It is therefore entirely possible that after a thread calls the yield method to pause, it immediately obtains the processor resources to be executed.
The 3.sleep method is more portable than the yield method, and it is generally not recommended to use the yield method to control the execution of concurrent threads.