当前位置:网站首页>详解LockSupport的使用
详解LockSupport的使用
2022-07-01 21:43:00 【键盘歌唱家】
一、简介
1.下图是Java api中的描述内容:
2.它的方法有以下几个,其中被圈出来的方法是核心方法:
3.唤醒和阻塞线程的常用方法中包含了LockSupport:
二、LockSupport唤醒阻塞方法的优势
1.Object类提供的wait和notify需要在synchronized代码块中使用,否则会报IllegalMonitorStateException异常 !并且notify必须在wait方法后面执行才能够生效 。(下面是正确的用法!)
public static void main(String[] args) {
Object objectlock = new Object();
new Thread(()->{
synchronized (objectlock){
System.out.println(Thread.currentThread().getName()+":\tcome in~");
try {
objectlock.wait();} catch (InterruptedException e) {
e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+": \t被唤醒");
}
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
new Thread(()->{
synchronized (objectlock) {
objectlock.notify();
System.out.println(Thread.currentThread().getName()+": \t发出通知!");
}
},"B").start();
}
2.ReentrantLock类提供的await和signal依旧存在执行顺序的问题,下面的案例让signal方法先执行,结果发现后执行的await并没有被唤醒!
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(()->{
//暂停1秒,让B线程先执行!
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+":\tcome in~");
condition.await();
System.out.println(Thread.currentThread().getName()+": \t被唤醒");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
},"A").start();
new Thread(()->{
lock.lock();
try {
condition.signal();
System.out.println(Thread.currentThread().getName()+": \t发出通知!");
}finally {
lock.unlock();
}
},"B").start();
}

3.LockSupport就不存在上面的问题,类似高速公路的ETC,提前买好了通行证unpark,到闸机处直接抬起栏杆放行了没有park拦截了。但unpark重复执行也只能积累一次通行证,所以下面的代码如果将注释掉的部分都去掉,那么A线程就会被阻塞!
public static void main(String[] args) {
Thread a = new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"休眠1s");
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
LockSupport.park();
//LockSupport.park();
System.out.println(Thread.currentThread().getName()+"解除阻塞成功!!!");
}, "A");
a.start();
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"发放通行证!!");
LockSupport.unpark(a);
//LockSupport.unpark(a);
},"B").start();
}

边栏推荐
- 【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
- Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
- Training on the device with MIT | 256Kb memory
- 焱融看 | 混合云时代下,如何制定多云策略
- 上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
- Pytest collection (2) - pytest operation mode
- PCB线路板塞孔工艺的那些事儿~
- Fundamentals - IO intensive computing and CPU intensive computing
- 工控设备安全加密的意义和措施
- Significance and measures of security encryption of industrial control equipment
猜你喜欢

Flume面试题

Spark面试题

Why does blocprovider feel similar to provider?

基于YOLOv5的口罩佩戴检测方法
![比较版本号[双指针截取自己想要的字串]](/img/19/4f858ffdc1281d6b8b18a996467f10.png)
比较版本号[双指针截取自己想要的字串]
![[noip2013] building block competition [noip2018] road laying greed / difference](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[noip2013] building block competition [noip2018] road laying greed / difference

业务可视化-让你的流程图'Run'起来

100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖

Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue

十三届蓝桥杯B组国赛
随机推荐
Separate the letters and numbers in the string so that the letters come first and the array comes last
require与import的区别和使用
AirServer手机第三方投屏电脑软件
Case of camera opening by tour
[monomer] recommended configuration of streaming information i-bpsv3 server
Several ways of writing main function in C
Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
PCB线路板塞孔工艺的那些事儿~
Getting started with the lockust series
Aidl basic use
Basic operation of binary tree
微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
I received a letter from CTO inviting me to interview machine learning engineer
Business visualization - make your flowchart'run'up
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
Test cancellation 1
【生态伙伴】鲲鹏系统工程师培训
最近公共祖先离线做法(tarjan)
基于K-means的用户画像聚类模型
String type conversion BigDecimal, date type