對于多線程編程而言,理解線程的生命周期非常重要,本文就針對這一點進行講解。
一、線程的狀態
線程的存在有幾種不同的狀態,如下:
- New狀態
- Ready狀態
- Running狀態
- Dead狀態
- Non Runnable狀態
1、New狀態
New狀態是線程已經被創建,但是還未開始運行的狀態。此狀態通過調用線程的start()方法可讓線程運行。
2、Runnable狀態
Runnable狀態可稱為準備運行狀態,也可稱為隊列,此狀態通過調用線程的start()方法可讓線程運行。
線程調度器決定要運行哪些線程,且線程運行多久。
3、Running狀態
如果一個線程正在執行中,那么它處于Running狀態。
4、Dead狀態
一旦某個線程進入了Dead狀態,那么它就再也不能運行了。
5、Non runnable狀態
某個正在運行的線程可轉變到Non runnable狀態,這取決于運行情況。
某個線程還可以一直保持Non runnable狀態,直到滿足的條件出現。
某個Non runnable狀態的線程不能直接跳轉到運行狀態,而是必須先轉變為Runnable狀態。
睡眠Sleeping:線程睡眠指定的時間。
I/O阻塞:線程等待,直到阻塞操作的完成。
join阻塞:線程等待,直到另一個線程執行完成。
等待通知:線程等待另一個線程的通知。
鎖機制阻塞:線程等待,直到指定的鎖被釋放,獲得鎖。
Java虛擬機JVM根據線程的優先級和調度原則執行線程。
二、線程調度器
在JVM中,線程調度器的實現通常基于以下兩種策略:
- 搶占式調度策略
- 分時調度策略或Round-robin循環調度策略
線程調度器的實現與平臺無關,因此線程的調度是不可預測的。
三、線程的優先級
JVM會為每一個新創建的線程分配一個優先級。
- 0級:這是最低的優先級
- 5級:這是普通的優先級
- 10級:這是最高的優先級
為了保存這些值,線程類有三個相應的變量:
- public static final int MIN_PRIORITY
- public static final int NORM_PRIORITY
- public static final int MAX_PRIORITY
一個線程首先會繼承其父線程的優先級,每一個線程默認的優先級是5級(Normal優先級),主線程的默認優先級為5級。
可以通過setPriority(int priority)方法來設置線程的優先級。
- public final void setPriority(int priority)
- public void getPriority();
用戶定義的線程,其默認的線程名為Thread+序號,序號從0開始,比如第一個線程為Thread0。
線程名可以通過setName(String name)方法進行設置,可使用getName()方法獲得線程的名字。
- public final void setName(String name)
- public final String getName().
實例
下面看一個例子:
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
|
package demo.ch; public class UserThread extends Thread { UserThread() { super (); } UserThread(String name) { super (name); } public void run() { System.out.println( "thread started running.." ); } public static void main(String[] args) { UserThread thread1 = new UserThread( "Thread1" ); UserThread thread2 = new UserThread( "Thread2" ); System.out.println( "Thread 1 initial name and priority" ); System.out.println( "name:" + thread1.getName()); System.out.println( "priority:" + thread1.getPriority()); System.out.println( "Thread 2 initial name and priority" ); System.out.println( "name:" + thread2.getName()); System.out.println( "priority:" + thread2.getPriority()); System.out.println( "" ); thread1.setPriority( 6 ); thread2.setPriority( 9 ); System.out.println( "Thread 1 initial name and priority" ); System.out.println( "name:" + thread1.getName()); System.out.println( "priority:" + thread1.getPriority()); System.out.println( "Thread 2 initial name and priority" ); System.out.println( "name:" + thread2.getName()); System.out.println( "priority:" + thread2.getPriority()); System.out.println( "" ); thread1.start(); thread2.start(); for ( int i= 0 ; i< 5 ; i++) System.out.println( "main method i value: " + i); } } |
輸出結果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Thread 1 initial name and priority name:Thread1 priority: 5 Thread 2 initial name and priority name:Thread2 priority: 5 Thread 1 initial name and priority name:Thread1 priority: 6 Thread 2 initial name and priority name:Thread2 priority: 9 main method i value: 0 main method i value: 1 thread started running.. main method i value: 2 thread started running.. main method i value: 3 main method i value: 4 |
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/chszs/article/details/50058129