当前位置:网站首页>Unsafe中的park和unpark
Unsafe中的park和unpark
2022-06-27 06:29:00 【不穿铠甲的穿山甲】
一、方法如下
public native void unpark(Object var1);
public native void park(boolean var1, long var2);
二、使用说明
park:将当前线程挂起。unpark:精准的唤醒某个线程。
park的参数,表示挂起的到期时间,第一个如果是true,表示绝对时间,则var2为绝对时间值,单位是毫秒。第一个参数如果是false,表示相对时间,则var2为相对时间值,单位是纳秒。
unpark的参数,表示线程。
简单示例:
park(false,0) 表示永不到期,一直挂起,直至被唤醒
long time = System.currentTimeMillis()+3000;
park(true,time + 3000) 表示3秒后自动唤醒
park(false,3000000000L) 表示3秒后自动唤醒
三、测试示例
package com.suncy.article.article5;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class ParkTest {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InterruptedException {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
//线程1必须等待唤醒
Thread thread1 = new Thread(() -> {
System.out.println("线程1:执行任务");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("线程1:挂起,等待唤醒才能继续执行任务");
unsafe.park(false, 0);
System.out.println("线程1:执行完毕");
});
thread1.start();
//线程2必须等待唤醒
Thread thread2 = new Thread(() -> {
System.out.println("线程2:执行任务");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("线程2:挂起,等待唤醒才能继续执行任务");
unsafe.park(false, 0);
System.out.println("线程2:执行完毕");
});
thread2.start();
Thread.sleep(5000);
System.out.println("唤醒线程2");
unsafe.unpark(thread2);
Thread.sleep(1000);
System.out.println("唤醒线程1");
unsafe.unpark(thread1);
//线程3自动唤醒
Thread thread3 = new Thread(() -> {
System.out.println("线程3:执行任务");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("线程3:挂起,等待时间到自动唤醒");
unsafe.park(false, 3000000000L);
System.out.println("线程3:执行完毕");
});
thread3.start();
//线程4自动唤醒
Thread thread4 = new Thread(() -> {
System.out.println("线程4:执行任务");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("线程4:挂起,等待时间到自动唤醒");
long time = System.currentTimeMillis() + 3000;
unsafe.park(true, time);
System.out.println("线程4:执行完毕");
});
thread4.start();
}
}
测试结果:

四、目的
1、使用Unsafe中的park和unpark和上篇文章说的《Unsafe中的CAS》可以完成一个自己的锁,这应该是并发编程基础的前提条件。
作者:空白小小格
链接:https://www.jianshu.com/p/15f33406543b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Fast realization of Bluetooth communication between MCU and mobile phone
- Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
- JVM garbage collection mechanism
- NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
- 观测电机转速转矩
- Yaml file encryption
- LeetCode 0086. Separate linked list
- HTAP 快速上手指南
- 快速实现蓝牙iBeacn功能详解
- Tidb basic functions
猜你喜欢

The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree

Yaml file encryption

427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)

JVM整体结构解析

浅谈GPU:历史发展,架构

高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression

Caldera installation and simple use

matlab GUI界面仿真直流电机和交流电机转速仿真

观测电机转速转矩

分数阶PID控制
随机推荐
可扩展哈希
Dev++ environment setting C language keyword display color
HTAP Quick Start Guide
Centos7.9安装mysql 5.7,并设置开机启动
路由器和交换机的区别
美摄云服务方案:专为轻量化视频制作场景打造
下载cuda和cudnn
[QT notes] simple understanding of QT meta object system
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
第 299 场周赛 第四题 6103. 从树中删除边的最小分数
Us camera cloud service scheme: designed for lightweight video production scenes
G1和ZGC垃圾收集器
Run opcua protocol demo on raspberry pie 4B to access kubeedge
The number of query results of maxcompute SQL is limited to 1W
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
Thesis reading skills
JVM类加载机制
解决 Win10 Wsl2 IP 变化问题
On gpu: historical development and structure
JVM object composition and storage