當(dāng)線程被創(chuàng)建并啟動(dòng)之后,它既不是一啟動(dòng)就進(jìn)入執(zhí)行狀態(tài),也不是一直處于執(zhí)行狀態(tài),在其生命周期中,要經(jīng)過“新建(New)”、“就緒(Runnable)”、“運(yùn)行(Running')”、“阻塞(Blocked)”和“死亡(Dead)”五種狀態(tài)。線程在創(chuàng)建之后,不可能一直霸占著CPU獨(dú)立運(yùn)行,需要在多個(gè)線程之間切換,所以大部分時(shí)間處于運(yùn)行、阻塞之間切換。
一、線程的狀態(tài)
線程的存在有幾種不同的狀態(tài),如下:
- New狀態(tài)
- Ready狀態(tài)
- Running狀態(tài)
- Dead狀態(tài)
- Non Runnable狀態(tài)
1、New狀態(tài)
New狀態(tài)是線程已經(jīng)被創(chuàng)建,但是還未開始運(yùn)行的狀態(tài)。此狀態(tài)通過調(diào)用線程的start()方法可讓線程運(yùn)行。
2、Runnable狀態(tài)
Runnable狀態(tài)可稱為準(zhǔn)備運(yùn)行狀態(tài),也可稱為隊(duì)列,此狀態(tài)通過調(diào)用線程的start()方法可讓線程運(yùn)行。
線程調(diào)度器決定要運(yùn)行哪些線程,且線程運(yùn)行多久。
3、Running狀態(tài)
如果一個(gè)線程正在執(zhí)行中,那么它處于Running狀態(tài)。
4、Dead狀態(tài)
一旦某個(gè)線程進(jìn)入了Dead狀態(tài),那么它就再也不能運(yùn)行了。
5、Non runnable狀態(tài)
- 某個(gè)正在運(yùn)行的線程可轉(zhuǎn)變到Non runnable狀態(tài),這取決于運(yùn)行情況。
- 某個(gè)線程還可以一直保持Non runnable狀態(tài),直到滿足的條件出現(xiàn)。
- 某個(gè)Non runnable狀態(tài)的線程不能直接跳轉(zhuǎn)到運(yùn)行狀態(tài),而是必須先轉(zhuǎn)變?yōu)镽unnable狀態(tài)。
- 睡眠Sleeping:線程睡眠指定的時(shí)間。
- I/O阻塞:線程等待,直到阻塞操作的完成。
- join阻塞:線程等待,直到另一個(gè)線程執(zhí)行完成。
- 等待通知:線程等待另一個(gè)線程的通知。
- 鎖機(jī)制阻塞:線程等待,直到指定的鎖被釋放,獲得鎖。
Java虛擬機(jī)JVM根據(jù)線程的優(yōu)先級(jí)和調(diào)度原則執(zhí)行線程。
二、線程調(diào)度器
在JVM中,線程調(diào)度器的實(shí)現(xiàn)通常基于以下兩種策略:
搶占式調(diào)度策略
分時(shí)調(diào)度策略或Round-robin循環(huán)調(diào)度策略
線程調(diào)度器的實(shí)現(xiàn)與平臺(tái)無關(guān),因此線程的調(diào)度是不可預(yù)測(cè)的。
三、線程的優(yōu)先級(jí)
JVM會(huì)為每一個(gè)新創(chuàng)建的線程分配一個(gè)優(yōu)先級(jí)。
0級(jí):這是最低的優(yōu)先級(jí)
5級(jí):這是普通的優(yōu)先級(jí)
10級(jí):這是最高的優(yōu)先級(jí)
為了保存這些值,線程類有三個(gè)相應(yīng)的變量:
- public static final int MIN_PRIORITY
- public static final int NORM_PRIORITY
- public static final int MAX_PRIORITY
一個(gè)線程首先會(huì)繼承其父線程的優(yōu)先級(jí),每一個(gè)線程默認(rèn)的優(yōu)先級(jí)是5級(jí)(Normal優(yōu)先級(jí)),主線程的默認(rèn)優(yōu)先級(jí)為5級(jí)。
可以通過setPriority(int priority)方法來設(shè)置線程的優(yōu)先級(jí)。
public final void setPriority(int priority)
public void getPriority();
用戶定義的線程,其默認(rèn)的線程名為Thread+序號(hào),序號(hào)從0開始,比如第一個(gè)線程為Thread0。
線程名可以通過setName(String name)方法進(jìn)行設(shè)置,可使用getName()方法獲得線程的名字。
public final void setName(String name)
public final String getName().
實(shí)例
下面看一個(gè)例子:
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); } } |
輸出結(jié)果:
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 |
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!