Java Concurrent Programming: how to block and wake up multithreading

Time:2021-1-21

Thread blocking and wake-up is a key point in the process of multithreading concurrency. When the number of threads reaches a large order of magnitude, concurrency may bring many hidden problems. How to pause a thread correctly and how to resume at a required time point after the pause need careful consideration. Java provides us with a variety of APIs to block and wake threads, such as suspend and resume, sleep, wait and notify, and park and unpark.

Java Concurrent Programming: how to block and wake up multithreading

sleep

The simplest way to control thread blocking and wake-up is sleep. Java can make thread enter blocking waiting state through sleep (n) method, and wake up automatically after the sleep time reaches the specified value. This method is simple and commonly used, but it is rather rigid, so we need to determine the time of thread blocking in advance. In some scenes, we can’t determine the sleep time at all, which is the biggest disadvantage of sleep mode.

Java Concurrent Programming: how to block and wake up multithreading

The use of sleep is very simple. Here is an example. Let the current thread sleep 2000ms, and the final output is “sleep time in MS = 2000”.

Java Concurrent Programming: how to block and wake up multithreading

Suspend and resume

In the history of Java, suspend() and resume() methods were used to block and wake threads. It can control the time nodes of blocking and waking in code, which is more flexible than sleep() method. For example, after a thread starts, it needs to be suspended at a certain time point. This can be done by using the suspend method, and when it needs to be awakened again, it can be done by using the resume method.

Java Concurrent Programming: how to block and wake up multithreading

Java Concurrent Programming: how to block and wake up multithreading

Note: methods such as suspend(), resume(), stop() are marked as expired methods, because they do not guarantee the release of resources and are prone to deadlock, so they are not recommended.

Deadlock problem

Java Concurrent Programming: how to block and wake up multithreading

Why does this happen? It’s actually caused by a deadlock. At first glance, it seems that there is no problem at all. The task of thread is simply to print string. In fact, the root of the problem is deeply hidden. After the main thread starts the thread MT, the thread MT starts to execute the execute () method and continuously prints the string.

The problem is that System.out.println Because println is declared as a synchronization method, synchronization lock will be applied to the singleton property of out (an instance of printstream class) of system class during execution. The suspend () method suspends the thread but does not release the lock. After the thread MT is suspended, the main thread calls System.out.println In order to print “can you get here?”, it is also necessary to obtain the synchronization lock of UT object of system class. The main thread has been waiting for the synchronization lock, but the MT thread does not release the lock, which leads to the deadlock.

Java Concurrent Programming: how to block and wake up multithreading