/ Java  

Java Multithreading 5: Sleep

This blog will introduce the sleep() method in Thread. The content includes:

  1. Introduction to sleep()
  2. Sleep() example
  3. Comparison of sleep() and wait()

Introduction to sleep()

sleep() is defined in Thread.java.

sleep() will make the current thread sleep, that is, the current thread will enter the blocked state (sleep) from the running state. sleep() will specify the sleep time, the thread sleep time will be greater than/equal to the sleep time. When the thread is woken up again, it will change from blocked state (sleep) to runnable state, thereby waiting for the CPU to execute.

sleep() example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);

if (i%4 == 0)
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public class SleepDemo{
public static void main(String[] args){
ThreadA t1 = new ThreadA("t1");
t1.start();
}
}

Results:

1
2
3
4
5
6
7
8
9
10
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t1: 5
t1: 6
t1: 7
t1: 8
t1: 9

Start thread t1 in the main thread. After t1 starts, when the calculation i in t1 is divisible by 4, t1 will sleep for 100 milliseconds through Thread.sleep(100).

Comparison of sleep() and wait()

We know that wait() will let the current thread enter the blocked state (wait) from the running state, and also release the synchronization lock. sleep() will make the current thread enter the blocked state (sleep) from the running state.

However, wait() will release the synchronization lock of the object, while sleep() will not release the lock.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class SleepDemo {
private static final 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() {
// Acquire synchronization lock of obj
synchronized (obj) {
try {
for (int i = 0; i < 10; i++) {
System.out.printf("%s: %d\n", this.getName(), i);

if (i % 4 == 0)
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

Results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

Threads t1 and t2 are started in the main thread. t1 and t2 will reference the synchronization lock of the same object in run(), namely synchronized(obj). While t1 is running, although it will call Thread.sleep(100), t2 will not get cpu execution rights, as t1 did not release the synchronous lock of obj!

Note that if we comment out synchronized(obj) and execute the program again, t1 and t2 can be switched to each other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
t1: 0
t2: 0
t1: 1
t1: 2
t1: 3
t1: 4
t2: 1
t2: 2
t2: 3
t2: 4
t2: 5
t2: 6
t2: 7
t2: 8
t1: 5
t1: 6
t1: 7
t1: 8
t2: 9
t1: 9