我来滥竽充数一篇文章,LockSupport
和synchronized
果然不能混着用,直接造成死锁。
package test;
import java.util.concurrent.locks.LockSupport;
/**
* @author Wei.Chou(weichou2010@gmail.com)
* @version 1.0, 12/08/2016
*/
public class ParkTest {
public static void main(String[] args) {
final Thread thread = new Thread() {
@Override
public void run() {
System.out.println("thread started");
synchronized (ParkTest.class) {
System.out.println("thread park");
LockSupport.park();
System.out.println("thread release");
}
}
};
thread.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread() {
@Override
public void run() {
System.out.println("thread1 started");
// TODO: 16/8/12 果然死锁
synchronized (ParkTest.class) {
System.out.println("thread1 unpark");
LockSupport.unpark(thread);
System.out.println("thread1 end");
}
}
}.start();
}
}