Java中線程的創(chuàng)建有兩種方式:
1. 通過繼承Thread類,重寫Thread的run()方法,將線程運行的邏輯放在其中
2. 通過實現(xiàn)Runnable接口,實例化Thread類
在實際應(yīng)用中,我們經(jīng)常用到多線程,如車站的售票系統(tǒng),車站的各個售票口相當(dāng)于各個線程。當(dāng)我們做這個系統(tǒng)的時候可能會想到兩種方式來實現(xiàn),繼承Thread類或?qū)崿F(xiàn)Runnable接口,現(xiàn)在看一下這兩種方式實現(xiàn)的兩種結(jié)果。
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
|
package com.threadtest; class MyThread extends Thread{ private int ticket = 10 ; private String name; public MyThread(String name){ this .name =name; } public void run(){ for ( int i = 0 ;i< 500 ;i++){ if ( this .ticket> 0 ){ System.out.println( this .name+ "賣票---->" +( this .ticket--)); } } } } public class ThreadDemo { public static void main(String[] args) { MyThread mt1= new MyThread( "一號窗口" ); MyThread mt2= new MyThread( "二號窗口" ); MyThread mt3= new MyThread( "三號窗口" ); mt1.start(); mt2.start(); mt3.start(); } } |
運行結(jié)果如下:
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
|
一號窗口賣票----> 10 一號窗口賣票----> 9 二號窗口賣票----> 10 一號窗口賣票----> 8 一號窗口賣票----> 7 一號窗口賣票----> 6 三號窗口賣票----> 10 一號窗口賣票----> 5 一號窗口賣票----> 4 一號窗口賣票----> 3 一號窗口賣票----> 2 一號窗口賣票----> 1 二號窗口賣票----> 9 二號窗口賣票----> 8 三號窗口賣票----> 9 三號窗口賣票----> 8 三號窗口賣票----> 7 三號窗口賣票----> 6 三號窗口賣票----> 5 三號窗口賣票----> 4 三號窗口賣票----> 3 三號窗口賣票----> 2 三號窗口賣票----> 1 二號窗口賣票----> 7 二號窗口賣票----> 6 二號窗口賣票----> 5 二號窗口賣票----> 4 二號窗口賣票----> 3 二號窗口賣票----> 2 二號窗口賣票----> 1 |
通過實現(xiàn)Runnable接口的代碼如下:
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.threadtest; class MyThread1 implements Runnable{ private int ticket = 10 ; private String name; public void run(){ for ( int i = 0 ;i< 500 ;i++){ if ( this .ticket> 0 ){ System.out.println(Thread.currentThread().getName()+ "賣票---->" +( this .ticket--)); } } } } public class RunnableDemo { public static void main(String[] args) { // TODO Auto-generated method stub //設(shè)計三個線程 MyThread1 mt = new MyThread1(); Thread t1 = new Thread(mt, "一號窗口" ); Thread t2 = new Thread(mt, "二號窗口" ); Thread t3 = new Thread(mt, "三號窗口" ); // MyThread1 mt2 = new MyThread1(); // MyThread1 mt3 = new MyThread1(); t1.start(); t2.start(); t3.start(); } } |
運行結(jié)果如下:
1
2
3
4
5
6
7
8
9
10
|
一號窗口賣票----> 10 三號窗口賣票----> 9 三號窗口賣票----> 7 三號窗口賣票----> 5 三號窗口賣票----> 4 三號窗口賣票----> 3 三號窗口賣票----> 2 三號窗口賣票----> 1 一號窗口賣票----> 8 二號窗口賣票----> 6 |
為什么會出現(xiàn)這種結(jié)果吶。我們不妨做個比喻,其實剛的程序,
繼承Thread類的,我們相當(dāng)于拿出三件事即三個賣票10張的任務(wù)分別分給三個窗口,他們各做各的事各賣各的票各完成各的任務(wù),因為MyThread繼承Thread類,所以在new MyThread的時候在創(chuàng)建三個對象的同時創(chuàng)建了三個線程;
實現(xiàn)Runnable的, 相當(dāng)于是拿出一個賣票10張得任務(wù)給三個人去共同完成,new MyThread相當(dāng)于創(chuàng)建一個任務(wù),然后實例化三個Thread,創(chuàng)建三個線程即安排三個窗口去執(zhí)行。
用圖表示如下:
在我們剛接觸的時候可能會迷糊繼承Thread類和實現(xiàn)Runnable接口實現(xiàn)多線程,其實在接觸后我們會發(fā)現(xiàn)這完全是兩個不同的實現(xiàn)多線程,一個是多個線程分別完成自己的任務(wù),一個是多個線程共同完成一個任務(wù)。
其實在實現(xiàn)一個任務(wù)用多個線程來做也可以用繼承Thread類來實現(xiàn)只是比較麻煩,一般我們用實現(xiàn)Runnable接口來實現(xiàn),簡潔明了。
大多數(shù)情況下,如果只想重寫 run() 方法,而不重寫其他 Thread 方法,那么應(yīng)使用 Runnable 接口。這很重要,因為除非程序員打算修改或增強類的基本行為,否則不應(yīng)為該類(Thread)創(chuàng)建子類。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://mars914.iteye.com/blog/1508429