一個任務在正常情況下是按順序執(zhí)行的,但是如果當前任務里有多個相似進程塊(例如for,while語句),我們就可以考慮把這些代碼塊抽出來并行運行,無需阻塞
2.實現(xiàn)多線程的幾種方式
一種是繼承thread類重寫run方法,另一種是實現(xiàn)runnable接口重寫run方法
啟動多線程很多情況下是為了處理并發(fā)進程,此時對于部分實時性要求不是那么高的業(yè)務需求,我們還可以通過實現(xiàn)隊列的方式,異步實現(xiàn)。
3.舉例
繼承thread
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * * @classname: threadbyex * @description: todo * @author mr.jqcheng * @date 2018年9月26日 * */ public class threadbyex extends thread{ @override public void run() { // todo auto-generated method stub system.out.println( "我是繼承線程" ); } } |
實現(xiàn)runnable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * * @classname: threadbyrunnable * @description: todo * @author mr.jqcheng * @date 2018年9月26日 * */ public class threadbyrunnable implements runnable{ /*public threadbyrunnable() { this.run(); // todo auto-generated constructor stub }*/ public void run() { // todo auto-generated method stub system.out.println( "我是實現(xiàn)進程" ); } } |
測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * * @classname: test * @description: todo * @author mr.jqcheng * @date 2018年9月26日 * */ public class test { public static void main(string[] args) { // 繼承thread啟動的方法 threadbyex t1 = new threadbyex(); t1.start(); // 啟動線程 // 實現(xiàn)runnable啟動線程的方法 threadbyrunnable r = new threadbyrunnable(); thread t2 = new thread(r); t2.start(); // 啟動線程 //new threadbyrunnable(); } } |
運行結果:
我是繼承線程
我是實現(xiàn)進程
ok,簡單的多線程實現(xiàn)方式完成了,在調用start()的時候,該進程已經進入可執(zhí)行狀態(tài),等待系統(tǒng)執(zhí)行。
線程處理的幾個常用方法:
void interrupt():向線程發(fā)送中斷請求,線程的中斷狀態(tài)將會被設置為true,如果當前線程被一個sleep調用阻塞,那么將會拋出interrupedexception異常。
static boolean interrupted():測試當前線程(當前正在執(zhí)行命令的這個線程)是否被中斷。注意這是個靜態(tài)方法,調用這個方法會產生一個副作用那就是它會將當前線程的中斷狀態(tài)重置為false。
boolean isinterrupted():判斷線程是否被中斷,這個方法的調用不會產生副作用即不改變線程的當前中斷狀態(tài)。
static thread currentthread() : 返回代表當前執(zhí)行線程的thread對象。
守護進程
用來服務于不是服務進程的其他所有當前進程下的所有線程
實現(xiàn)deamon.setdaemon(true)就行,要在線程開啟之前啟用
舉例
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
|
package com.orange.util; /** * * @classname: test * @description: todo * @author mr.jqcheng * @date 2018年9月26日 * */ public class test { public static void main(string[] args) { thread deamon2 = new thread( new daemonrunner2(), "otherrunner" ); deamon2.start(); // 啟動線程 try { thread.sleep( 1000 ); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } thread deamon = new thread( new daemonrunner(), "daemonrunner" ); // 設置為守護線程 deamon.setdaemon( true ); deamon.start(); // 啟動線程 } static class daemonrunner implements runnable { public void run() { // todo auto-generated method stub try { thread.sleep( 300 ); thread t = thread.currentthread(); system.out.println(t); } catch (exception e) { e.printstacktrace(); } finally { system.out.println( "進入守護線程,說明現(xiàn)在還有其他線程在執(zhí)行" ); } } } static class daemonrunner2 implements runnable { public void run() { // todo auto-generated method stub try { thread.sleep( 1500 ); system.out.println( "我是其他線程" ); } catch (exception e) { e.printstacktrace(); } } } } |
執(zhí)行結果:
thread[daemonrunner,5,main]
進入守護線程,說明現(xiàn)在還有其他線程在執(zhí)行
我是其他線程
首先,先啟動其他線程,需要耗時1500ms,同時,主線程耗時1000ms后,開始進入守護線程,此時其它線程還在運行,到了守護線程,耗時300ms,其他線程仍在執(zhí)行,繼續(xù)往下,守護線程執(zhí)行完畢
但是如果我把守護線程的300ms改成500ms,會發(fā)生什么事呢?
出現(xiàn)過兩種情況,畢竟在臨界值
1.我是其他線程
2.thread[daemonrunner,5,main]
進入守護線程,說明現(xiàn)在還有其他線程在執(zhí)行
我是其他線程