LockSupport

是一个很简单的类:

底层通过CAS操作实现

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        ThreadA a = new ThreadA("a");
        a.start();
        Thread.sleep(1000);
        LockSupport.unpark(a);
    }
}
class ThreadA extends Thread{
    public ThreadA(String name){
        super(name);
    }
    @Override
    public void run() {
        String threadName = this.getName();
        System.out.println(threadName +    "准备park");
        LockSupport.park();
        System.out.println(threadName +    "继续运行");
    }
}

结果:

a准备park
a继续运行

 

posted @ 2018-11-26 18:32  式微胡不归  阅读(120)  评论(0编辑  收藏  举报