import java.util.concurrent.locks.LockSupport;
/**
* 线程阻塞工具类:LockSupport
* 可以在线程内任意位置让线程阻塞
*/
public class LockSupportDemo {
public static Object o = 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);
}
@Override
public void run() {
synchronized (o){
System.out.println("in "+getName());
LockSupport.park();//阻塞当前线程
}
}
}
public static void main(String[] args) throws InterruptedException{
t1.start();
Thread.sleep(100);
t2.start();
// LockSupport.unpark(t1);
// LockSupport.unpark(t2);
t1.join();
t2.join();
}
//虽然我们无法保证unpark()方法发生在park()方法之后,但是执行这段代码,它自始至终都可以正常的结束
//,不会因为park()方法而导致线程永久性的挂起
//这是因为LockSupport类使用类似信号量的机制。它为每个线程准备了一个许可,如果许可可用,那么park()
//函数会立即返回,并且消费这个许可(将许可变为不可用),如果许可不可用,就会阻塞。而unpark()使一个许可变为可用
//(与信号量不同,许可不能累加,不可能拥有超过一个许可)
}
import java.util.concurrent.locks.LockSupport;
/**
* 除了阻塞的功能,LockSupport.park()还支持中断响应,但不会抛出异常,它只是默默的返回
* 可以从Thread.interrupted()等方法获取中断标记
*/
public class LockSupportDemo2 {
public static Object o = 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);
}
@Override
public void run() {
synchronized (o){
System.out.println("in "+getName());
LockSupport.park();
if (Thread.interrupted()){
System.out.println(getName()+" 被中断了");
}
}
System.out.println(getName()+"执行结束");
}
}
public static void main(String[] args) throws InterruptedException{
t1.start();
Thread.sleep(100);
t2.start();
t1.interrupt();//中断了处于park()状态的t1。之后,t1可以马上响应这个中断,并且返回,
// 之后在外面等待的t2才可以进入临界区,并最终由LockSupport.unpark(t2);操作使其运行结束
LockSupport.unpark(t2);
//in t1
//t1 被中断了
//t1执行结束
//in t2
//t2执行结束
}
}