The contents involved in this chapter include:
1. Introduction to sleep()
2. Sleep() example
3. Comparison between sleep() and wait()
1. Introduction to sleep()
sleep() is defined in Thread.java.
The function of sleep() is to let the current thread sleep, that is, the current thread will enter from the "running state" to the "sleep (blocking) state". sleep() will specify the sleep time, and the thread sleep time will be greater than/equal to the sleep time; when the thread is awakened again, it will change from a "blocking state" to a "ready state", waiting for the CPU to be scheduled to execute.
2. Sleep() example
The following is a simple example to demonstrate the usage of sleep().
The code copy is as follows:
// Source code of SleepTest.java
class ThreadA extends Thread{
public ThreadA(String name){
super(name);
}
public synchronized void run() {
try {
for(int i=0; i <10; i++){
System.out.printf("%s: %d/n", this.getName(), i);
// When i can be divided by 4, sleep for 100 milliseconds
if (i%4 == 0)
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class SleepTest{
public static void main(String[] args){
ThreadA t1 = new ThreadA("t1");
t1.start();
}
}
Running results:
The code copy is as follows:
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t1: 5
t1: 6
t1: 7
t1: 8
t1: 9
Results description:
The program is relatively simple, start thread t1 in the main thread main. After t1 is started, when the calculation i in t1 can be divisible by 4, t1 will sleep for 100 milliseconds through Thread.sleep(100).
Comparison between sleep() and wait()
We know that the function of wait() is to allow the current thread to enter the "wait (blocking) state from the "running state" and also release the synchronization lock. The function of sleep() is to let the current thread enter the "sleep (blocking) state" from the "running state".
However, wait() releases the object's synchronization lock, while sleep() does not release the lock.
The following example shows that sleep() will not release the lock.
The code copy is as follows:
// Source code of SleepLockTest.java
public class SleepLockTest{
private static Object obj = new Object();
public static void main(String[] args){
ThreadA t1 = new ThreadA("t1");
ThreadA t2 = new ThreadA("t2");
t1.start();
t2.start();
}
static class ThreadA extends Thread{
public ThreadA(String name){
super(name);
}
public void run(){
// Get the synchronization lock of the obj object
synchronized (obj) {
try {
for(int i=0; i <10; i++){
System.out.printf("%s: %d/n", this.getName(), i);
// When i can be divided by 4, sleep for 100 milliseconds
if (i%4 == 0)
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Running results:
The code copy is as follows:
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t1: 5
t1: 6
t1: 7
t1: 8
t1: 9
t2: 0
t2: 1
t2: 2
t2: 3
t2: 4
t2: 5
t2: 6
t2: 7
t2: 8
t2: 9
Results description:
Two threads t1 and t2 are started in the main thread main. t1 and t2 will refer to the synchronization lock of the same object in run(), that is, synchronized(obj). During t1 running, although it will call Thread.sleep(100); t2 will not obtain CPU execution rights. Because t1 does not release the "synchronous lock held by obj"!
Note that if we comment out synchronized (obj) and execute the program again, t1 and t2 can be switched to each other. The following is the source code after the comment tune synchronized(obj):
The code copy is as follows:
// SleepLockTest.java source code (commented out synchronized(obj))
public class SleepLockTest{
private static Object obj = new Object();
public static void main(String[] args){
ThreadA t1 = new ThreadA("t1");
ThreadA t2 = new ThreadA("t2");
t1.start();
t2.start();
}
static class ThreadA extends Thread{
public ThreadA(String name){
super(name);
}
public void run(){
// Get the synchronization lock of the obj object
// synchronized (obj) {
try {
for(int i=0; i <10; i++){
System.out.printf("%s: %d/n", this.getName(), i);
// When i can be divided by 4, sleep for 100 milliseconds
if (i%4 == 0)
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// }
}
}
}