当前位置:网站首页>Multithreading (2) -- pipeline (4) -- Park and unpark
Multithreading (2) -- pipeline (4) -- Park and unpark
2022-06-12 06:19:00 【leo_ messi94】
Basic use
They are LockSupport Methods in class
// Pause current thread
LockSupport.park();
// Resume the running of a thread
LockSupport.unpark;
Situation 1 : First park Again unPark:
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
try {
log.debug("====start");
Thread.sleep(1000);
log.debug("====park");
// The thread becomes wait state
LockSupport.park();
log.debug("====resume");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
Thread.sleep(2000);
log.debug("====unpark");
LockSupport.unpark(t1);
}
result :
10:57:28.529 [Thread-0] DEBUG park - ====start
10:57:29.546 [Thread-0] DEBUG park - ====park
10:57:30.529 [main] DEBUG park - ====unpark
10:57:30.530 [Thread-0] DEBUG park - ====resume
Situation two : First unpark stay park:
modify 2 Time sleep Time for
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
try {
log.debug("====start");
Thread.sleep(2000);
log.debug("====park");
LockSupport.park();
log.debug("====resume");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
Thread.sleep(1000);
log.debug("====unpark");
LockSupport.unpark(t1);
}
result :
11:03:13.374 [Thread-0] DEBUG park - ====start
11:03:14.371 [main] DEBUG park - ====unpark
11:03:15.379 [Thread-0] DEBUG park - ====park
11:03:15.379 [Thread-0] DEBUG park - ====resume
characteristic
And Object Of wait and notify comparison
- wait,notify and notifyall Must cooperate Object Monitor Use it together , and unpark Unwanted
- park and unpark Is to block and wake up threads on a thread by thread basis , and notify Only one waiting thread can be wakened randomly ,notifyAll Is to wake up all waiting threads , Not so precise
- park and unpark You can start with unpark, and wait and notify Not first notify
principle
Each thread has its own Parker object , It's made up of three parts _counter, _cond and _mutex
give an example :
- To make an analogy, thread is like a traveler ,Parker Like his backpack , Condition variables, _ cond It's like a tent in a backpack ._counter It's like spare dry food in a backpack (0 To exhaust ,1 For the sake of sufficiency )
- call park It depends on whether you need to stop and rest
- If the spare dry food runs out , So get into the tent and rest
- If there's enough dry food in reserve , Then there is no need to stay , To move forward
- call unpark, It's like having enough dry food
- If the thread is still in the tent , Just wake him up and let him move on
- If the thread is still running , So next time he calls park when , Just consume spare dry food , No need to stop and move on
- Because the backpack space is limited , Multiple calls unpark Only one spare dry food will be added
Explain the above 1: First call park, Calling unpark
1. call park:
- Current thread call Unsafe.park() Method
- Check _counter , This situation is 0, At this time , get _mutex The mutex (mutex Object has a waiting queue _cond)
- Thread entry _cond Conditional variables block
- Set up _counter = 0

2. call unpark:
- call Unsafe.unpark(Thread_0) Method , Set up _counter by 1
- unpark To blocking queue _cond in , Wake up the _cond In a conditional variable Thread_0
- Thread_0 Resume operation
- Set up _counter by 0

Explain the above 2: First call unpark, Call again park
- call Unsafe.unpark(Thread_0) Method , Set up _counter by 1
- Current thread call Unsafe.park() Method
- Check _counter , This situation is 1, At this time, the thread does not need to block , Continue operation
- Set up _counter by 0

If we call continuously 2 Time park, Will cause the thread to block :
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
try {
log.debug("====start");
Thread.sleep(2000);
log.debug("====park");
LockSupport.park();
LockSupport.park();
log.debug("====resume");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
Thread.sleep(1000);
log.debug("====unpark");
LockSupport.unpark(t1);
}
result :
边栏推荐
- RNN model
- [reinstall system] 01 system startup USB flash disk production
- C2w model - language model
- Why doesn't the database use binary tree, red black tree, B tree and hash table? Instead, a b+ tree is used
- Sqlite Cross - compile Dynamic Library
- Project and build Publishing
- JS pre parsing
- Leetcode 第 80 場雙周賽題解
- Une explication du 80e match bihebdomadaire de leetcode
- Leetcode-717. 1-bit and 2-bit characters (O (1) solution)
猜你喜欢

(UE4 4.27) customize globalshader

How do I get the date and time from the Internet- How to get DateTime from the internet?

Nocturnal simulator ADB view log

(UE4 4.27) UE4 adds a customized meshpass to realize the edge illumination of the mobile terminal

Guns framework multi data source configuration without modifying the configuration file

关于 Sensor flicker/banding现象的解释

Word2Vec

MNIST handwritten data recognition by RNN

Trunet: short videos generation from long videos via story preserving truncation (thesis translation)

Dlib face detection
随机推荐
PDF. js FAQs
JS预解析
Unity surface shader with template buffer
Using hidden Markov model to mark part of speech
Poisson disk sampling for procedural placement
Leetcode-1604. Warning people who use the same employee card more than or equal to three times within one hour
QT--实现TCP通信
Houdini terrain creation
勤于奋寻找联盟程序方法介绍
468. verifying the IP address
线程有哪些状态?
Piecewise Bezier curve
Project and build Publishing
Automatic modeling of Interchange
Video summary with long short term memory
Une explication du 80e match bihebdomadaire de leetcode
Leetcode 第 80 场双周赛题解
CONDA create use virtual environment
E-book analysis
JS variable scope