当前位置:网站首页>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
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
- TiDB 中的数据库模式概述
- 第 299 场周赛 第四题 6103. 从树中删除边的最小分数
- 426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
- Scala之偏函数Partial Function
- NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
- 【LeetCode】Day90-二叉搜索树中第K小的元素
- 2022 CISP-PTE(一)文件包含
- Instance Tunnel 使用
- Currying Scala functions
猜你喜欢
Modeling competition - optical transport network modeling and value evaluation
Run opcua protocol demo on raspberry pie 4B to access kubeedge
G1 and ZGC garbage collector
AHB2APB桥接器设计(2)——同步桥设计的介绍
On gpu: historical development and structure
Caldera安装及简单使用
使用 WordPress快速个人建站指南
winow10安装Nexus nexus-3.20.1-01
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)
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
随机推荐
Fractional Order PID control
论文阅读技巧
What's new in redis4.0 - active memory defragmentation
The restart status of the openstack instance will change to the error handling method. The openstack built by the container restarts the compute service method of the computing node and prompts the gi
路由器和交换机的区别
线程间等待与唤醒机制、单例模式、阻塞队列、定时器
HTAP 快速上手指南
[cultivation system] common regular expressions
卷积神经网络---CNN模型的应用(找矿预测)
观测电机转速转矩
高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression
winow10安装Nexus nexus-3.20.1-01
Quick realization of Bluetooth ibeacn function
Dev++ environment setting C language keyword display color
Distribution gaussienne, régression linéaire, régression logistique
426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)
TiDB 中的数据库模式概述
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)
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
Once spark reported an error: failed to allocate a page (67108864 bytes), try again