当前位置:网站首页>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 .
边栏推荐
- Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
- multiprocessing. Detailed explanation of pool
- Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
- 解决 Win10 Wsl2 IP 变化问题
- ORA-00909: 参数个数无效,concat引起
- 浅谈GPU:历史发展,架构
- Restrictions on the use of tidb
- Transaction overview of tidb
- Quick personal site building guide using WordPress
- Proxy reflect usage details
猜你喜欢

Dev++ environment setting C language keyword display color

Caldera安装及简单使用

面试官:请你介绍一下缓存穿透、缓存空值、缓存雪崩、缓存击穿的,通俗易懂

2022 le fichier CISP - Pte (i) contient:

Machine learning

Information System Project Manager - Chapter VII project cost management

Matlab GUI interface simulation DC motor and AC motor speed simulation

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

Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes

Active learning
随机推荐
POI 替换docx中的文字和图片
Spark sql 常用时间函数
HTAP in depth exploration Guide
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
vs怎么配置OpenCV?2022vs配置OpenCV详解(多图)
Unsafe中的park和unpark
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
可扩展哈希
TiDB的事务概览
[QT notes] simple understanding of QT meta object system
从5秒优化到1秒,系统飞起来了...
记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
Assembly language - Wang Shuang Chapter 11 flag register - Notes
One year's experience of technical personnel in Entrepreneurship
面试官:你天天用 Lombok,说说它什么原理?我竟然答不上来…
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
winow10安装Nexus nexus-3.20.1-01
Modeling competition - optical transport network modeling and value evaluation
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
Tidb basic functions