微吼云上線多路互動(dòng)直播服務(wù) 加速多場(chǎng)景互動(dòng)直播落地
658
2025-03-31
文章目錄
概述
線程的生命周期
線程優(yōu)先級(jí)
創(chuàng)建線程的兩種方式
通過實(shí)現(xiàn)Runnable接口創(chuàng)建一個(gè)線程
通過擴(kuò)展Thread類創(chuàng)建一個(gè)線程
線程的主要操作
Thread.stop()廢棄原因
Thread.suspend()、resume()廢棄原因
線程間通信
主要方法
概述
多線程程序包含兩個(gè)或多個(gè)可同時(shí)運(yùn)行的部分,每個(gè)部分可以同時(shí)處理不同的任務(wù),從而能更好地利用可用資源,特別是當(dāng)計(jì)算機(jī)有多個(gè)CPU的時(shí)候。
多線程夠?qū)懭攵鄠€(gè)活動(dòng),可以在同一程序中同時(shí)進(jìn)行操作處理。
根據(jù)定義,多任務(wù)是當(dāng)多個(gè)進(jìn)程共享,如CPU處理公共資源。
多線程將多任務(wù)的概念擴(kuò)展到可以將單個(gè)應(yīng)用程序中的特定操作細(xì)分為單個(gè)線程的應(yīng)用程序。每個(gè)線程可以并行運(yùn)行。
操作系統(tǒng)不僅在不同的應(yīng)用程序之間劃分處理時(shí)間,而且在應(yīng)用程序中的每個(gè)線程之間劃分處理時(shí)間。
多線程能夠在同一程序同中,進(jìn)行多個(gè)活動(dòng)的方式進(jìn)行寫入。
線程的生命周期
線程從創(chuàng)建到運(yùn)行完成要經(jīng)歷下面這些階段:
New
Runnable
Running
Non-Runnable (blocked) / Waiting
Terminated
New: 該狀態(tài)指已經(jīng)創(chuàng)建了該線程類的實(shí)例,但是還沒有調(diào)用 start() 方法。
Runnable: 該狀態(tài)指線程已經(jīng)調(diào)用了 start() 方法,已經(jīng)可以運(yùn)行了,正在等待線程調(diào)度器來運(yùn)行。
Running: 該狀態(tài)指此線程已經(jīng)被線程調(diào)度器選中并正在運(yùn)行中。
Non-Runnable (blocked): 該狀態(tài)指線程還存在但是沒有資格運(yùn)行。可能的原因有:sleep 操作、等待文件 I/O 的完成或等待釋放鎖狀態(tài)等等。
Terminated: 該狀態(tài)指線程已經(jīng)運(yùn)行結(jié)束,處于中止?fàn)顟B(tài), run() 方法已經(jīng)退出。
線程優(yōu)先級(jí)
每個(gè)Java線程都有一個(gè)優(yōu)先級(jí),可以幫助操作系統(tǒng)確定安排線程的順序。
Java線程優(yōu)先級(jí)在MIN_PRIORITY(常數(shù)為1)和MAX_PRIORITY(常數(shù)為10)之間的范圍內(nèi)。
默認(rèn)情況下,每個(gè)線程都被賦予優(yōu)先級(jí)NORM_PRIORITY(常數(shù)為5)。
具有較高優(yōu)先級(jí)的線程對(duì)于一個(gè)程序來說更重要,應(yīng)該在低優(yōu)先級(jí)線程之前分配處理器時(shí)間。 然而,
線程優(yōu)先級(jí)不能保證線程執(zhí)行的順序,并且依賴于平臺(tái)。
創(chuàng)建線程的兩種方式
通過實(shí)現(xiàn)Runnable接口創(chuàng)建一個(gè)線程
第一步:
實(shí)現(xiàn)由Runnable接口提供的run()方法。 該方法為線程提供了一個(gè)入口點(diǎn),該方法中存放完整的業(yè)務(wù)邏輯。
public void run()
1
第二步:
實(shí)例化一個(gè)Thread對(duì)象,通過Thread提供的以下構(gòu)造函數(shù)
Thread(Runnable threadObj, String threadName);
1
threadObj:實(shí)現(xiàn)Runnable接口的類的實(shí)例
threadName :新線程的名稱
第三步:
創(chuàng)建了一個(gè)線程對(duì)象,可以通過調(diào)用start()方法啟動(dòng)它,該方法執(zhí)行對(duì)run()方法的調(diào)用.
package com.xgj.master.java.concurrency; public class RunnableDemo implements Runnable { private String threadName; private Thread thread; /** * * @Title:CreateThreadByRunnable * @Description:構(gòu)造函數(shù) * @param threadName */ public RunnableDemo(String threadName) { this.threadName = threadName; System.out.println("Create thread:" + threadName); } /** * * @Title: run * @Description: 重寫Runnable接口中的run方法,業(yè)務(wù)邏輯存在與該方法中 * @return: void */ @Override public void run() { System.out.println("Running " + threadName ); try { for (int i = 0 ; i < 5 ; i++) { System.out.println("ThreadName:" + threadName + " : " + i); // 睡眠0.5秒 Thread.sleep(500); } } catch (InterruptedException e) { e.printStackTrace(); System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } /** * * @Title: startThread * @Description: 如果線程對(duì)象為空,創(chuàng)建并啟動(dòng)線程 ,通過調(diào)用thread.start()方法啟動(dòng)線程,該方法執(zhí)行對(duì)run()方法的調(diào)用. * @return: void */ public void startThread(){ if( thread == null){ thread = new Thread(this, threadName); thread.start(); } } }
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
package com.xgj.master.java.concurrency; public class RunnableDemoTest { public static void main(String[] args) { RunnableDemo thread1 = new RunnableDemo("thread-1"); thread1.startThread(); RunnableDemo thread2 = new RunnableDemo("thread-2"); thread2.startThread(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
運(yùn)行結(jié)果(每次運(yùn)行的結(jié)果不盡相同)
Create thread:thread-1 Create thread:thread-2 Running thread-1 ThreadName:thread-1 : 0 Running thread-2 ThreadName:thread-2 : 0 ThreadName:thread-1 : 1 ThreadName:thread-2 : 1 ThreadName:thread-1 : 2 ThreadName:thread-2 : 2 ThreadName:thread-1 : 3 ThreadName:thread-2 : 3 ThreadName:thread-2 : 4 ThreadName:thread-1 : 4 Thread thread-1 exiting. Thread thread-2 exiting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
通過擴(kuò)展Thread類創(chuàng)建一個(gè)線程
我們通過繼承Thread類,來重寫上面的例子
package com.xgj.master.java.concurrency; public class ThreadDemo extends Thread { private Thread t; private String threadName; /** * * @Title:ThreadDemo * @Description:構(gòu)造函數(shù),實(shí)例化一個(gè)帶有ThreadName的線程 * @param threadName */ public ThreadDemo(String threadName) { this.threadName = threadName; System.out.println("Creating " + threadName); } /** * * @Title: run * @Description: 業(yè)務(wù)邏輯存在與該方法中 * @return: void */ public void run() { System.out.println("Running " + threadName); try { for (int i = 0; i < 5; i++) { System.out.println("Thread: " + threadName + ", " + i); // 睡眠0.5秒 Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } /** * * @Title: startThread * @Description: 如果線程對(duì)象為空,創(chuàng)建并啟動(dòng)線程 ,通過調(diào)用thread.start()方法啟動(dòng)線程,該方法執(zhí)行對(duì)run()方法的調(diào)用. * @return: void */ public void startThread() { System.out.println("Starting " + threadName); if (t == null) { t = new Thread(this, threadName); t.start(); } } }
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
package com.xgj.master.java.concurrency; public class ThreadDemoTest { public static void main(String[] args) { ThreadDemo threadDemo1 = new ThreadDemo("Thread-1"); threadDemo1.start(); ThreadDemo threadDemo2 = new ThreadDemo("Thread-2"); threadDemo2.start(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
運(yùn)行結(jié)果(每次運(yùn)行的結(jié)果不盡相同)
Creating Thread-1 Creating Thread-2 Running Thread-1 Thread: Thread-1, 0 Running Thread-2 Thread: Thread-2, 0 Thread: Thread-2, 1 Thread: Thread-1, 1 Thread: Thread-1, 2 Thread: Thread-2, 2 Thread: Thread-2, 3 Thread: Thread-1, 3 Thread: Thread-2, 4 Thread: Thread-1, 4 Thread Thread-1 exiting. Thread Thread-2 exiting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
線程的主要操作
已廢棄 public void suspend() 該方法使線程處于掛起狀態(tài),可以使用resume()方法恢復(fù)。
已廢棄 public void stop() 該方法使線程完全停止。
已廢棄 public void resume() 該方法恢復(fù)使用suspend()方法掛起的線程。
public void wait() 導(dǎo)致當(dāng)前線程等到另一個(gè)線程調(diào)用notify()。
public void notify() 喚醒在此對(duì)象監(jiān)視器上等待的單個(gè)線程。
JDK1.2之后已經(jīng)不再使用suspend(),resume()和stop()方法。
Thread.stop()廢棄原因
stop方法會(huì)解除被加鎖的對(duì)象的鎖,因而可能造成這些對(duì)象處于不一致的狀態(tài),而且這個(gè)方法造成的ThreadDeath異常不像其他的檢查期異常一樣被捕獲。
可以使用interrupt方法代替。
事實(shí)上,如果一個(gè)方法不能被interrupt,那stop方法也不會(huì)起作用。
Thread.suspend()、resume()廢棄原因
suspend/resume方法容易發(fā)生死鎖。
使用suspend時(shí),并不會(huì)釋放鎖;假設(shè)先獲取該鎖,再進(jìn)行resume,就會(huì)造成死鎖。
可以使用Object的wait和notify方法代替。wait方法會(huì)釋放持有的鎖。
線程間通信
主要方法
public void wait() 使當(dāng)前線程等到另一個(gè)線程調(diào)用notify()方法。
public void notify() 喚醒在此對(duì)象監(jiān)視器上等待的單個(gè)線程。
public void notifyAll() 喚醒所有在同一個(gè)對(duì)象上調(diào)用wait()的線程。
上述幾個(gè)方法方法已被實(shí)現(xiàn)為Object中的final方法,因此它們?cè)谒蓄愔卸伎捎谩?所有這三種方法只能從同步上下文中調(diào)用。
Java 任務(wù)調(diào)度
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。