当前位置:网站首页>Park and unpark in unsafe
Park and unpark in unsafe
2022-06-27 06:49:00 【Pangolin without armor】
One 、 The method is as follows
public native void unpark(Object var1);
public native void park(boolean var1, long var2);
Two 、 Instructions
park: Suspends the current thread .unpark: Wake up a thread accurately .
park Parameters of , Indicates the pending expiration time , If the first one is true, Indicates absolute time , be var2 Is the absolute time value , In milliseconds . The first parameter is if false, It means relative time , be var2 Is the relative time value , The unit is nanosecond .
unpark Parameters of , Represents a thread .
A simple example :
park(false,0) Means never to expire , Always on hold , Until awakened
long time = System.currentTimeMillis()+3000;
park(true,time + 3000) Express 3 Wake up automatically in seconds
park(false,3000000000L) Express 3 Wake up automatically in seconds
3、 ... and 、 Test examples
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);
// Threads 1 Must wait for awakening
Thread thread1 = new Thread(() -> {
System.out.println(" Threads 1: Perform tasks ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(" Threads 1: Hang up , Wait for wakeup to continue the task ");
unsafe.park(false, 0);
System.out.println(" Threads 1: completion of enforcement ");
});
thread1.start();
// Threads 2 Must wait for awakening
Thread thread2 = new Thread(() -> {
System.out.println(" Threads 2: Perform tasks ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(" Threads 2: Hang up , Wait for wakeup to continue the task ");
unsafe.park(false, 0);
System.out.println(" Threads 2: completion of enforcement ");
});
thread2.start();
Thread.sleep(5000);
System.out.println(" Wake up the thread 2");
unsafe.unpark(thread2);
Thread.sleep(1000);
System.out.println(" Wake up the thread 1");
unsafe.unpark(thread1);
// Threads 3 Wake up automatically
Thread thread3 = new Thread(() -> {
System.out.println(" Threads 3: Perform tasks ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(" Threads 3: Hang up , Wait for the time to wake up automatically ");
unsafe.park(false, 3000000000L);
System.out.println(" Threads 3: completion of enforcement ");
});
thread3.start();
// Threads 4 Wake up automatically
Thread thread4 = new Thread(() -> {
System.out.println(" Threads 4: Perform tasks ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(" Threads 4: Hang up , Wait for the time to wake up automatically ");
long time = System.currentTimeMillis() + 3000;
unsafe.park(true, time);
System.out.println(" Threads 4: completion of enforcement ");
});
thread4.start();
}
}
test result :

Four 、 Purpose
1、 Use Unsafe Medium park and unpark And the last article said 《Unsafe Medium CAS》 Can complete a lock of its own , This should be a prerequisite for the foundation of concurrent programming .
author : Small blank space
link :https://www.jianshu.com/p/15f33406543b
source : Simple books
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- Once spark reported an error: failed to allocate a page (67108864 bytes), try again
- Compatibility comparison between tidb and MySQL
- LeetCode 0086. Separate linked list
- Tidb database Quick Start Guide
- 获取地址url中的query参数指定参数方法
- ORA-00909: 参数个数无效,concat引起
- 浅谈GPU:历史发展,架构
- Keep 2 decimal places after multiplying SQLSEVER fields
- 2022 CISP-PTE(一)文件包含
- 技术人员创业一年心得
猜你喜欢

Ahb2apb bridge design (2) -- Introduction to synchronous bridge design

第 299 场周赛 第四题 6103. 从树中删除边的最小分数

Mathematical modeling contest for graduate students - optimal application of UAV in rescue and disaster relief

Modeling competition - optical transport network modeling and value evaluation

Machine learning

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

Proxy reflect usage details

win10远程连接云服务器

OPPO面试整理,真正的八股文,狂虐面试官

快速实现单片机和手机蓝牙通信
随机推荐
Assembly language - Wang Shuang Chapter 11 flag register - Notes
第 299 场周赛 第四题 6103. 从树中删除边的最小分数
Unsafe中的park和unpark
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
内存屏障今生之Store Buffer, Invalid Queue
poi导出excle
Unrecognized VM option ‘‘
Gaussian distribution, linear regression, logistic regression
IDEA中关于Postfix Completion代码模板的一些设置
Spark sql 常用时间函数
C Primer Plus Chapter 11_ Strings and string functions_ Codes and exercises
pytorch Default process group is not initialized
Write an example of goroutine and practice Chan at the same time
[cultivation system] common regular expressions
技术人员创业一年心得
The risk of multithreading -- thread safety
TiDB 基本功能
Idea one click log generation
Spark SQL common time functions
TiDB的事务概览