3.1.7 线程阻塞工具类:LockSupport

package 第三章.线程阻塞工具LockSupport;

import java.util.concurrent.locks.LockSupport;

/**
* Created by zzq on 2018/2/2.
*/
public class LockSupportDemo {
public static Object u = new Object();
static ChangeObjectThread t1 = new ChangeObjectThread("t1");
static ChangeObjectThread t2 = new ChangeObjectThread("t2");

public static class ChangeObjectThread extends Thread {
public ChangeObjectThread(String name) {
super.setName(name);
}

public void run() {
synchronized (u) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("in " + getName());
LockSupport.park();
System.out.println("out " + getName());
}
}
}


public static void main(String args[]) throws InterruptedException {
t1.start();
Thread.sleep(3000);
t2.start();
LockSupport.unpark(t1);
LockSupport.unpark(t2);
t1.join();
t2.join();
}
}
posted @ 2018-02-07 10:48  anxbb  阅读(135)  评论(0编辑  收藏  举报