The thread's Sleep method signature bit:
public static void sleep (long Millis) throws Interruptexception is a static method that causes the currently executing thread to hibernate millis milliseconds
Package Com.demo;
Class Mythread implements runnable{public
void Run (
int i = 0; i < 4; i++) {
try {
System.out.pri Ntln (Thread.CurrentThread (). GetName () + "Sleep 0.5 seconds!");
Thread.Sleep (+);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println ("Currently running thread Name:" + Thread.CurrentThread (). GetName ());
}}} public class demo {public
static void Main (string[] args) {
mythread mt1 = new Mythread ();
Thread th = new Thread (MT1, "thread A");
System.out.println ("\ n Thread is starting");
Th.start ();
try {
thread.sleep (1000);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println ("\ n main thread is already dormant 2s" + thread.currentthread (). GetName ());
}
The results of the operation are as follows:
Thread is starting
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A
Main thread has been dormant 1s main
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A
From the results of the operation can be analyzed, when the thread a hibernate 1s, the main thread is also dormant 1s, the current running thread is main thread main, and then thread a continues to run.