實現(xiàn)多線程的方式:
實現(xiàn)多線程的方式有多種,這里只列舉兩種常用的,而第一種繼承thread的方式無法實現(xiàn)多窗口賣票。
一,繼承thread方式:
特點:多線程多實例,無法實現(xiàn)資源的共享。
例子:
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
|
package com.demo.study.multithreading; public class mythread extends thread{ private int i = 10 ; // 可以自行定義鎖,也可以使用實例的鎖 object mutex = new object(); public void selltickets(){ synchronized (mutex) { if (i> 0 ){ i--; //getname()獲取線程的名字 system.out.println(thread.currentthread().getname()+ " :" + i); } } } @override public void run() { while (i> 0 ){ selltickets(); } } } |
啟動線程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.demo.study.multithreading; public class test { public static void main(string[] args) { //繼承thread方式:多線程多實例,無法實現(xiàn)資源的共享 mythread mythread1 = new mythread(); mythread mythread2 = new mythread(); //給線程命名 mythread1.setname( "線程1" ); mythread2.setname( "線程2" ); mythread1.start(); mythread2.start(); } } |
運行結(jié)果:
二,實現(xiàn)runnable方式:
特點:多線程單實例,可實現(xiàn)資源的共享
例子:實現(xiàn)多窗口賣票:
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
|
package com.demo.study.multithreading; public class mythreadimpl implements runnable { private int tickets = 10 ; public void selltickets() { synchronized (mythreadimpl. class ) { if (tickets > 0 ) { tickets--; system.out.println(thread.currentthread().getname() + "正在賣票,還剩下" + tickets + "張" ); } } } @override public void run() { while (tickets > 0 ) { selltickets(); try { // 休眠一秒,讓執(zhí)行的效果更明顯 thread.sleep( 100 ); } catch (interruptedexception e) { e.printstacktrace(); } } } } |
啟動線程:
注意:thread中的start()方法是線程的就緒,而線程的啟動,需要等待cpu的調(diào)度(也就是所謂的線程搶資源);run()方法的方法體代表了線程需要完成的任務,稱之為線程執(zhí)行體。
void start() 使該線程開始執(zhí)行;java 虛擬機調(diào)用該線程的 run 方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.demo.study.multithreading; public class test { public static void main(string[] args) { //只創(chuàng)建一個實例 mythreadimpl threadimpl = new mythreadimpl(); //將上面創(chuàng)建的唯一實例放入多個線程中,thread類提供了多個構(gòu)造方法,見下圖(構(gòu)造方法摘要) thread thread1 = new thread(threadimpl, "窗口1" ); thread thread2 = new thread(threadimpl, "窗口2" ); thread1.start(); thread2.start(); } } |
構(gòu)造方法摘要 | |
---|---|
thread() 分配新的 thread 對象。 |
|
thread(runnable target) 分配新的 thread 對象。 |
|
thread(runnable target, string name) 分配新的 thread 對象。 |
|
thread(string name) 分配新的 thread 對象。 |
|
thread(threadgroup group, runnable target) 分配新的 thread 對象。 |
|
thread(threadgroup group, runnable target, string name) 分配新的 thread 對象,以便將 target 作為其運行對象,將指定的 name 作為其名稱,并作為 group 所引用的線程組的一員。 |
|
thread(threadgroup group, runnable target, string name, long stacksize) 分配新的 thread 對象,以便將 target 作為其運行對象,將指定的 name 作為其名稱,作為 group 所引用的線程組的一員,并具有指定的堆棧大小。 |
|
thread(threadgroup group, string name) 分配新的 thread 對象。 |
運行結(jié)果:
三、同步鎖與資源共享:
cpu可能隨機的在多個處于就緒狀態(tài)中的線程中進行切換,這時就可能出現(xiàn)線程的安全問題;線程安全問題,其實是指多線程環(huán)境下對共享資源的訪問可能會引起此共享資源的不一致性,而解決安全問題則需要同步鎖的加入,執(zhí)行synchronized部分代碼的時候必須需要對象鎖,而一個對象只有一個鎖,只有執(zhí)行完synchronized里面的代碼后釋放鎖,其他線程才可以獲得鎖,那么就保證了同一時刻只有一個線程訪問synchronized里面的代碼。實現(xiàn)資源共享的關鍵是,只有一個實例,synchronized使用的是同一把鎖,用實例的鎖或者定義一個實例。這就需要使用實現(xiàn)runnable接口的方式,實現(xiàn)多線程,這樣傳入的是一個實例。繼承thread的方式,傳入的是多個實例,每個實例都有一個鎖,那就無法實現(xiàn)控制。
以上這篇多線程(多窗口賣票實例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務器之家。