logo头像
书院的十三先生

Java并发编程入门(八)线程生命周期

一、线程生命周期一

线程生命周期如下:

1.线程创建后状态为NEW
2.线程启动后状态从NEW变为RUNNABLE
3.线程调用同步代码块未获得同步锁时状态为BLOCK,如果获取到锁则状态为RUNNABLE
4.线程运行结束后状态为TERMINATED

验证代码如下:

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
public class LifeCycleDemo {

public static void main(String[] args) {
Thread t0 = new Thread(new Runnable() {
public void run() {
Counter.increase();
}
});

Thread t1 = new Thread(new Runnable() {
public void run() {
Counter.increase();
}
});

//创建后状态为NEW
printState(t0);
printState(t1);

t0.start();
t1.start();

//启动后状态为RUNNABLE
printState(t0);
printState(t1);

sleep(1000);

//运行后t0获得同步锁,进入代码块执行Thread.sleep后状态变为TIMED_WAITING,t1等待获取同步锁,状态为BLOCKED
printState(t0);
printState(t1);

sleep(4000);

//线程执行完后状态为TERMINATED
printState(t0);
printState(t1);

System.out.println("count: " + Counter.getCount());
}

private static void printState(Thread t) {
System.out.println(t.getName() + " " + t.getState());
}

private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class Counter {
private static int count;

public synchronized static void increase() {
count = count +1;

try {
//模拟并发争用
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}

public static int getCount() {
return count;
}
}

打印日志:

1
2
3
4
5
6
7
8
9
Thread-0 NEW
Thread-1 NEW
Thread-0 RUNNABLE
Thread-1 RUNNABLE
Thread-0 BLOCKED
Thread-1 TIMED_WAITING
Thread-0 TERMINATED
Thread-1 TERMINATED
count: 2

二、线程生命周期二

验证代码如下:

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
public class LifeCycleDemo2 {
public static void main(String[] args) {
Thread t0 = new Thread(new ChildThread(Thread.currentThread()));

t0.start();
//子线程启动后状态为RUNNABLE
printState(t0);

try {
//子线程join后,主线程状态为WAITING
t0.join();

//主线程状态为RUNNABLE
printState(Thread.currentThread());

//子线程运行结束,状态为TERMINATED
printState(t0);

} catch (InterruptedException e) {
e.printStackTrace();
}
}

private static void printState(Thread t) {
System.out.println(t.getName() + " " + t.getState());
}
}

class ChildThread implements Runnable {

Thread parentThread;

public ChildThread(Thread parentThread) {
this.parentThread = parentThread;
}
public void run() {
try {
Thread.sleep(2000);
//这里是在子线程join之后打印,此时主线程状态为WAITING
System.out.println(parentThread.getName() + " " + parentThread.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

输出日志:

1
2
3
4
Thread-0 RUNNABLE
main WAITING
main RUNNABLE
Thread-0 TERMINATED

其他场景不在单独列出代码。

三、线程生命周期三


这里和前面的区别是方法参数中增加了时间参数,不再举例验证。

end.


站点: http://javashizhan.com/


微信公众号: