当前位置:网站首页>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 .
边栏推荐
- JVM tuning ideas
- 快速实现Thread Mesh组网详解
- 云服务器配置ftp、企业官网、数据库等方法
- Using CSDN to develop cloud and build navigation websites
- 2022 CISP-PTE(二)SQL注入
- An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
- 0.0.0.0:x的含义
- The number of query results of maxcompute SQL is limited to 1W
- tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
- [getting started] regular expression Basics
猜你喜欢

POI replacing text and pictures in docx

LeetCode 0086. Separate linked list

面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?

MPC control of aircraft wingtip acceleration and control surface

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

古典密码体制--代换和置换

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

一线大厂面试官问:你真的懂电商订单开发吗?

如何优雅的写 Controller 层代码?

2022 CISP-PTE(二)SQL注入
随机推荐
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
poi导出excle
LeetCode 0086. Separate linked list
机 器 学 习
研究生数学建模竞赛-无人机在抢险救灾中的优化应用
TiDB 中的SQL 基本操作
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
Distribution gaussienne, régression linéaire, régression logistique
2018 mathematical modeling competition - special clothing design for high temperature operation
聊聊领域驱动设计
快速实现Thread Mesh组网详解
Scala advanced_ Member access modifier
内存屏障今生之Store Buffer, Invalid Queue
快速实现蓝牙iBeacn功能详解
When there are multiple El select, the selected value is filtered by El select, and the last selected value is filtered by the second El select
Compatibility comparison between tidb and MySQL
Dev++ environment setting C language keyword display color
【OpenAirInterface5g】RRC NR解析之RrcSetupComplete
Assembly language - Wang Shuang Chapter 11 flag register - Notes
TiDB的事务概览