当前位置:网站首页>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
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- TiDB 中的SQL 基本操作
- 2022 CISP-PTE(一)文件包含
- Tar: /usr/local: cannot find tar in the Archive: due to the previous error, it will exit in the last error state
- 【LeetCode】Day90-二叉搜索树中第K小的元素
- JVM调优思路
- 日期 数据库日期 字符串 之间互相转换
- 网关状态检测 echo request/reply
- One year's experience of technical personnel in Entrepreneurship
- 310. minimum height tree
- 2018年数学建模竞赛-高温作业专用服装设计
猜你喜欢

Redis cache penetration, cache breakdown, cache avalanche

快速实现单片机和手机蓝牙通信

Fast realization of Bluetooth communication between MCU and mobile phone

On gpu: historical development and structure

Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes

IDEA中关于Postfix Completion代码模板的一些设置

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

Modeling competition - optical transport network modeling and value evaluation

thrift

Active learning
随机推荐
IDEA中关于Postfix Completion代码模板的一些设置
tracepoint
线程间等待与唤醒机制、单例模式、阻塞队列、定时器
TiDB的使用限制
SQL injection bypass (I)
下载cuda和cudnn
Multithreading basic Part3
SQL 注入绕过(一)
Fractional Order PID control
Altium designer 19 device silk screen label position shall be placed uniformly in batches
Transaction overview of tidb
[QT dot] realize the watchdog function to detect whether the external program is running
Configuration of vscode korofileheader
Convolution neural network -- Application of CNN model (ore prospecting prediction)
TiDB与 MySQL 兼容性对比
TiDB的事务概览
EasyExcel:读取Excel数据到List集合中
Currying Scala functions
机 器 学 习
HTAP 快速上手指南