-
Notifications
You must be signed in to change notification settings - Fork 243
/
ThreadLifeCycle.java
176 lines (138 loc) · 4.15 KB
/
ThreadLifeCycle.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package concurrency;
import java.util.concurrent.TimeUnit;
/**
* 线程的生命周期,演示线程的六种状态: NEW RUNNABLE TIMED_WAITING WAITING BLOCKED TERMINATED
*
* @author liu yuning
*
*/
class Block {
public boolean waitStatus = true;
public void waitOp() throws InterruptedException {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " 进入wait");
wait();
System.out.println(Thread.currentThread().getName()
+ " next to Block.wait(), in the loop");
}
}
public void notifyOp() {
synchronized (this) {
this.waitStatus = false;
notifyAll();
System.out.println(Thread.currentThread().getName()
+ " 向正在wait当前对象的线程发出notify");
}
}
}
class WaitRunnable implements Runnable {
private Block block;
public WaitRunnable(Block block) {
super();
this.block = block;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName()
+ " 被启动,开始执行...");
// 开始sleep
System.out.println(Thread.currentThread().getName()
+ " 开始sleep 2s");
TimeUnit.SECONDS.sleep(2);
System.out
.println(Thread.currentThread().getName() + " 从sleep中醒来");
// 从sleep中醒来后,进入wait状态
while (block.waitStatus) {
block.waitOp();
System.out.println(Thread.currentThread().getName()
+ " next to WaitRunnable.waitOp(), in the loop");
}
System.out.println(Thread.currentThread().getName()
+ " out of wait loop");
System.out.println(Thread.currentThread().getName()
+ " 任务执行结束,即将终止...");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class NotifyRunnable implements Runnable {
private Block block;
public NotifyRunnable(Block block) {
super();
this.block = block;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 被启动,开始执行...");
block.notifyOp();
synchronized (block) {
try {
System.out.println(Thread.currentThread().getName()
+ " 开始sleep 6s,并且不会释放当前对象的锁");
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " 从sleep中醒来");
}
}
public class ThreadLifeCycle {
private static final String IN_THE_STATE = "当前的状态为:";
private static final String ISALIVE_RETRUN = "isAlive()返回为:";
/**
* Describe the status of a thread
*
* @param threadName
* @param threadStatus
* @param isAlive
* @return thread status description
*/
public static String descStatus(String threadName, String threadStatus,
boolean isAlive) {
return threadName + " " + IN_THE_STATE + threadStatus + " "
+ ISALIVE_RETRUN + isAlive;
}
/**
* print the status of a thread
*
* @param thread
*/
public static void printStatus(Thread thread) {
System.out.println(descStatus(thread.getName(), thread.getState()
.toString(), thread.isAlive()));
}
public static void main(String[] args) throws InterruptedException {
// 该对象将作为后面加锁的对象
Block block = new Block();
// NEW
Thread thread = new Thread(new WaitRunnable(block));
Thread notifyThread = new Thread(new NotifyRunnable(block));
printStatus(thread);
printStatus(notifyThread);
// RUNNABLE
thread.start();
printStatus(thread);
TimeUnit.SECONDS.sleep(1);
// TIMED_WAITING
printStatus(thread);
TimeUnit.SECONDS.sleep(2);
// WAITING
printStatus(thread);
TimeUnit.SECONDS.sleep(1);
notifyThread.start();
printStatus(notifyThread);
TimeUnit.SECONDS.sleep(1);
// BLOCKED or TERMINATED
// TERMINATED:运行到此处时,如果notifyThread执行完notify操作后,调度器立马切换至thread的情况下,thread会先行终止,调度器再调度notifyThread
printStatus(thread);
printStatus(notifyThread);
TimeUnit.SECONDS.sleep(6);
// TERMINATED
printStatus(thread);
}
}