1、LockSupport:用这个锁来实现线程阻塞与释放,不要太好用
2、上demo:
public class T_LockSupport {
public static void main(String[] args) {
Thread thread = new Thread(()->{
for (int i = 0; i < 10; i++) {
System.out.println(i);
if (i == 5) {
LockSupport.park(); //在这里阻塞住
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
//主线程阻塞10秒以后,使得thread线程继续执行
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
LockSupport.unpark(thread);
}
}