当前位置:网站首页>Concurrent thread state transition
Concurrent thread state transition
2022-07-27 09:52:00 【Under the seven kingdoms, I want 99】
Concurrent thread state transition
- situation 1 NEW --> RUNNABLE
- situation 2 RUNNABLE <--> WAITING
- situation 3 RUNNABLE <--> WAITING
- situation 4 RUNNABLE <--> WAITING
- situation 5 RUNNABLE <--> TIMED_WAITING
- situation 6 RUNNABLE <--> TIMED_WAITING
- situation 7 RUNNABLE <--> TIMED_WAITING
- situation 8 RUNNABLE <--> TIMED_WAITING
- situation 9 RUNNABLE <--> BLOCKED
- situation 10 RUNNABLE <--> TERMINATED
Explain the transformation information according to the thread state transformation diagram sorted out before .

situation 1 NEW --> RUNNABLE
When calling t.start() When the method is used , from NEW --> RUNNABLE
situation 2 RUNNABLE <–> WAITING
t Thread use synchronized(obj) After getting the object lock
- call obj.wait() When the method is used ,t The thread from RUNNABLE --> WAITING
- call obj.notify() , obj.notifyAll() , t.interrupt() when
- The competition is successful ,t The thread from WAITING --> RUNNABLE
- The competition lock failed ,t The thread from WAITING --> BLOCKED
public class TestWaitNotify {
final static Object obj = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (obj) {
log.debug(" perform ....");
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug(" Other code ...."); // The breakpoint
}
},"t1").start();
new Thread(() -> {
synchronized (obj) {
log.debug(" perform ....");
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug(" Other code ...."); // The breakpoint
}
},"t2").start();
sleep(0.5);
log.debug(" Wake up the obj On other threads ");
synchronized (obj) {
obj.notifyAll(); // Wake up the obj All waiting threads on The breakpoint
}
}
}
/* Running results : 08:10:09.924 c.TestWaitNotify [t1] - perform .... 08:10:09.939 c.TestWaitNotify [t2] - perform .... 08:10:10.346 c.TestWaitNotify [main] - Wake up the obj On other threads 08:10:10.346 c.TestWaitNotify [t2] - Other code .... 08:10:10.346 c.TestWaitNotify [t1] - Other code .... */
explain :
1 When a thread t1 and t2 Successively compete to lock , Yes wait Method , State by RUNNABLE --> WAITING , Wait and release the lock .
2 Main thread sleep 0.5 Seconds later , Compete to lock , perform notifyAll Method , Wake up all other threads
3 t1 and t2 Threads compete for locks , A successful competition , State by WAITING --> RUNNABLE, Another competition failed , State by WAITING --> BLOCKED
4 t Release the lock after the thread executes , Another thread competes for the lock , Start execution , After execution , Thread by RUNNABLE <–> TERMINATED
situation 3 RUNNABLE <–> WAITING
Current thread call t.join() When the method is used , The current thread starts from RUNNABLE --> WAITING
- Is the current thread in t Wait on the monitor of the thread object
t End of thread run , Or called... Of the current thread interrupt() when , The current thread starts from WAITING --> RUNNABLE
situation 4 RUNNABLE <–> WAITING
Current thread call LockSupport.park() Method causes the current thread to run from RUNNABLE --> WAITING
call LockSupport.unpark( Target thread ) Or called a thread Of interrupt() , Will cause the target thread to run from WAITING --> RUNNABLE
situation 5 RUNNABLE <–> TIMED_WAITING
t Thread use synchronized(obj) After getting the object lock
- call obj.wait(long n) When the method is used ,t The thread from RUNNABLE --> TIMED_WAITING
- t Thread waiting time exceeded n millisecond , Or call obj.notify() , obj.notifyAll() , t.interrupt() when
- The competition is successful ,t The thread from TIMED_WAITING --> RUNNABLE
- The competition lock failed ,t The thread from TIMED_WAITING --> BLOCKED
situation 6 RUNNABLE <–> TIMED_WAITING
Current thread call t.join(long n) When the method is used , The current thread starts from RUNNABLE --> TIMED_WAITING
- Is the current thread in t Wait on the monitor of the thread object
The current thread has been waiting longer than n millisecond , or t End of thread run , Or called... Of the current thread interrupt() when , The current thread starts from TIMED_WAITING --> RUNNABLE
situation 7 RUNNABLE <–> TIMED_WAITING
Current thread call Thread.sleep(long n) , The current thread starts from RUNNABLE --> TIMED_WAITING
The current thread has been waiting longer than n millisecond , The current thread starts from TIMED_WAITING --> RUNNABLE
situation 8 RUNNABLE <–> TIMED_WAITING
Current thread call LockSupport.parkNanos(long nanos) or LockSupport.parkUntil(long millis) when , When the front line Cheng Cong RUNNABLE --> TIMED_WAITING
call LockSupport.unpark( Target thread ) Or called a thread Of interrupt() , Or waiting for a timeout , Will cause the target thread to run from TIMED_WAITING–> RUNNABLE
situation 9 RUNNABLE <–> BLOCKED
t Thread use synchronized(obj) If the race fails when an object lock is acquired , from RUNNABLE --> BLOCKED
a obj The synchronization block of the lock thread is executed , Will wake up all BLOCKED The threads of the , If one t Thread race success , from BLOCKED --> RUNNABLE , Other failed threads still BLOCKED
situation 10 RUNNABLE <–> TERMINATED
All the code of the current thread is running , Get into TERMINATED
边栏推荐
- July training (day 14) - stack
- July training (day 07) - hash table
- 去 OPPO 面试,被问麻了
- 35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction
- Qt 学习(二) —— .pro文件详解
- Redis 为什么这么快?Redis 的线程模型与 Redis 多线程
- When I went to oppo for an interview, I got numb
- 关于getter/setter方法问题
- Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children
- Understand chisel language. 25. Advanced input signal processing of chisel (I) -- asynchronous input and de jitter
猜你喜欢

监控神器:Prometheus 轻松入门,真香!

Nine ways to read the file path under the resources directory

Nacos configuration center dynamically refreshes the data source

Looking for a job for 4 months, interviewing 15 companies and getting 3 offers

Vscode uses remote SSH connection and the solution of connection failure

A ride into Qinchuan -- a brief talk on how beego Autorouter works

刷题《剑指Offer》day03

35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍

圆环工件毛刺(凸起)缺口(凹陷)检测案例
![[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination
随机推荐
聊聊索引失效的10种场景,太坑了
Nine ways to read the file path under the resources directory
刷题《剑指Offer》day04
Voice live broadcast system - Principles to be followed in developing push notifications
Eureka 延迟注册的一个坑
c'mon! Please don't ask me about ribbon's architecture principle during the interview
Understand chisel language. 26. Chisel advanced input signal processing (II) -- majority voter filtering, function abstraction and asynchronous reset
中高级试题」:MVCC 实现原理是什么?
Understand chisel language. 25. Advanced input signal processing of chisel (I) -- asynchronous input and de jitter
Understand chisel language. 24. Chisel sequential circuit (IV) -- detailed explanation of chisel memory
At the end of the year, I'll teach you how to get high performance!
Final examination paper of engineering materials
July training (day 16) - queue
Flash memory usage and stm32subemx installation tutorial [day 3]
深度剖析分库分表最强辅助Sharding Sphere
Nacos is used as a registration center
Summary of engineering material knowledge points (full)
July training (day 04) - greed
Understand chisel language. 27. Chisel advanced finite state machine (I) -- basic finite state machine (Moore machine)
July training (day 12) - linked list