-
Notifications
You must be signed in to change notification settings - Fork 160
/
ThreadState.java
64 lines (51 loc) · 1.48 KB
/
ThreadState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.concurrency.features.threadstate;
import com.example.concurrency.util.ThreadDumpHelper;
import com.example.concurrency.util.ThreadUtil;
/**
* 描述:
* 线程状态一览
*
* @author zed
*/
public class ThreadState {
private static ThreadDumpHelper threadDumpHelper = new ThreadDumpHelper();
public static void main(String[] args) {
new Thread(new TimeWaiting(),"TimeWaiting").start();
new Thread(new Waiting(),"Waiting").start();
new Thread(new Blocked(),"Blocked1").start();
new Thread(new Blocked(),"Blocked2").start();
threadDumpHelper.tryThreadDump();
}
static class TimeWaiting implements Runnable{
@Override
public void run() {
while (true){
ThreadUtil.sleep(1000);
}
}
}
static class Waiting implements Runnable{
@Override
public void run() {
while (true){
synchronized (Waiting.class){
try {
Waiting.class.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
}
static class Blocked implements Runnable{
@Override
public void run() {
while (true){
synchronized (Blocked.class){
ThreadUtil.sleep(1000);
}
}
}
}
}