首页 > 其他 > 详细

JUC之---超好用的阻塞锁

时间:2020-04-24 00:20:03      阅读:97      评论:0      收藏:0      [点我收藏+]

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);

}

}

JUC之---超好用的阻塞锁

原文:https://www.cnblogs.com/tengri-fighting/p/12764557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!