


Detailed explanation of thread blocking usage of LockSupport class in Java multi-thread programming
LockSupport is the basic thread blocking primitive used to create locks and other synchronization classes.
The functions of park() and unpark() in LockSupport are to block threads and unblock threads respectively, and park() and unpark() will not encounter deadlocks that may be caused by "Thread.suspend and Thread.resume" "question.
Because park() and unpark() have permissions; the race between the thread calling park() and another thread trying to unpark() it will remain active.
Basic usage
LockSupport is very similar to a binary semaphore (only 1 license is available for use). If this license has not been occupied, the current thread obtains the license and continues execution; if the license has been Occupied, the current thread is blocked, waiting to obtain permission.
public static void main(String[] args) { LockSupport.park(); System.out.println("block."); }
When you run this code, you can find that the main thread is always blocked. Because the license is occupied by default, the license cannot be obtained when calling park(), so it enters the blocking state.
The following code: release the permission first, then obtain the permission, and the main thread can terminate normally. The acquisition and release of LockSupport permissions are generally corresponding. If you unpark multiple times, there will be no problem with just one park. The result is that the permission is available.
public static void main(String[] args) { Thread thread = Thread.currentThread(); LockSupport.unpark(thread);//释放许可 LockSupport.park();// 获取许可 System.out.println("b"); }
LockSupport is not reentrant. If a thread calls LockSupport .park() twice in a row, the thread will definitely be blocked forever.
public static void main(String[] args) throws Exception { Thread thread = Thread.currentThread(); LockSupport.unpark(thread); System.out.println("a"); LockSupport.park(); System.out.println("b"); LockSupport.park(); System.out.println("c"); }
This code prints out a and b, but not c, because when park is called for the second time, the thread cannot obtain the permission and a deadlock occurs.
Let’s take a look at the responsiveness of LockSupport to interrupts
public static void t2() throws Exception { Thread t = new Thread(new Runnable() { private int count = 0; @Override public void run() { long start = System.currentTimeMillis(); long end = 0; while ((end - start) <= 1000) { count++; end = System.currentTimeMillis(); } System.out.println("after 1 second.count=" + count); //等待或许许可 LockSupport.park(); System.out.println("thread over." + Thread.currentThread().isInterrupted()); } }); t.start(); Thread.sleep(2000); // 中断线程 t.interrupt(); System.out.println("main over"); }
The final thread will print out thread over.true. This shows that if the thread is blocked by calling park, it can respond to the interrupt request (the interrupt status is set to true), but it will not throw InterruptedException.
LockSupport function list
// 返回提供给最近一次尚未解除阻塞的 park 方法调用的 blocker 对象,如果该调用不受阻塞,则返回 null。 static Object getBlocker(Thread t) // 为了线程调度,禁用当前线程,除非许可可用。 static void park() // 为了线程调度,在许可可用之前禁用当前线程。 static void park(Object blocker) // 为了线程调度禁用当前线程,最多等待指定的等待时间,除非许可可用。 static void parkNanos(long nanos) // 为了线程调度,在许可可用前禁用当前线程,并最多等待指定的等待时间。 static void parkNanos(Object blocker, long nanos) // 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。 static void parkUntil(long deadline) // 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。 static void parkUntil(Object blocker, long deadline) // 如果给定线程的许可尚不可用,则使其可用。 static void unpark(Thread thread)
##LockSupport example
Compare the following "Example 1" and "Example 2" to change Clearly understand the usage of LockSupport.
Example 1
public class WaitTest1 { public static void main(String[] args) { ThreadA ta = new ThreadA("ta"); synchronized(ta) { // 通过synchronized(ta)获取“对象ta的同步锁” try { System.out.println(Thread.currentThread().getName()+" start ta"); ta.start(); System.out.println(Thread.currentThread().getName()+" block"); // 主线程等待 ta.wait(); System.out.println(Thread.currentThread().getName()+" continue"); } catch (InterruptedException e) { e.printStackTrace(); } } } static class ThreadA extends Thread{ public ThreadA(String name) { super(name); } public void run() { synchronized (this) { // 通过synchronized(this)获取“当前对象的同步锁” System.out.println(Thread.currentThread().getName()+" wakup others"); notify(); // 唤醒“当前对象上的等待线程” } } } }
import java.util.concurrent.locks.LockSupport; public class LockSupportTest1 { private static Thread mainThread; public static void main(String[] args) { ThreadA ta = new ThreadA("ta"); // 获取主线程 mainThread = Thread.currentThread(); System.out.println(Thread.currentThread().getName()+" start ta"); ta.start(); System.out.println(Thread.currentThread().getName()+" block"); // 主线程阻塞 LockSupport.park(mainThread); System.out.println(Thread.currentThread().getName()+" continue"); } static class ThreadA extends Thread{ public ThreadA(String name) { super(name); } public void run() { System.out.println(Thread.currentThread().getName()+" wakup others"); // 唤醒“主线程” LockSupport.unpark(mainThread); } } }
main start ta main block ta wakup others main continue

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
