LockSupport工具类:
是java中锁的基类。
每一个线程都可与其建立许可联系。
默认情况下,一个线程是没有LockSupport的许可证的。
此时,如果调用LockSupport的静态方法park(),则当前线程会被挂起。
当时如果当前线程有许可证,就会直接返回。
当一个线程以线程1位参数调用unPark()方法时,会给线程1赋予许可证,并且如果线程1目前是处于因为park()而挂起的,则会被唤醒解除挂起。
package LockSupport;/*
name: demo01
user: ly
Date: 2020/5/30
Time: 11:43
*/
// 实例:LockSupport的pack()与unPark()
import java.util.concurrent.locks.LockSupport;
public class demo01 {
public static void main(String []args){
Thread thread = new Thread(new Runnable() {
public void run() {
System.out.println("the child thread is running...");
//自己挂起自己
LockSupport.park();
System.out.println("the child thread is over...");
}
});
thread.start();
System.out.println("main thread is running...");
//main线程唤醒child线程
LockSupport.unpark(thread); //如果注释掉,child thread就会永远挂起。
System.out.println("main thread is over");
}
}