概要
本章,我們學(xué)習(xí)“常用的實(shí)現(xiàn)多線程的2種方式”:Thread和Runnable。
之所以說是常用的,是因?yàn)橥ㄟ^還可以通過Java.util.concurrent包中的線程池來實(shí)現(xiàn)多線程。關(guān)于線程池的內(nèi)容,我們以后會(huì)詳細(xì)介紹;現(xiàn)在,先對(duì)的Thread和Runnable進(jìn)行了解。本章內(nèi)容包括:
轉(zhuǎn)載請(qǐng)注明出處:http://www.cnblogs.com/skywang12345/p/3479063.html
Thread和Runnable簡介
Runnable是一個(gè)接口,該接口中只包含了一個(gè)run()方法。它的定義如下:
public interface Runnable {
???? public abstract void run();
}
Runnable的作用,實(shí)現(xiàn)多線程。我們可以定義一個(gè)類A實(shí)現(xiàn)Runnable接口;然后,通過new Thread(new A())等方式新建線程。
Thread是一個(gè)類。Thread本身就實(shí)現(xiàn)了Runnable接口。它的聲明如下:
public class Thread implements Runnable {}
Thread的作用,實(shí)現(xiàn)多線程。
Thread和Runnable的異同點(diǎn)
Thread 和 Runnable 的相同點(diǎn):都是“多線程的實(shí)現(xiàn)方式”。
Thread 和 Runnable 的不同點(diǎn):
Thread 是類,而Runnable是接口;Thread本身是實(shí)現(xiàn)了Runnable接口的類。我們知道“一個(gè)類只能有一個(gè)父類,但是卻能實(shí)現(xiàn)多個(gè)接口”,因此Runnable具有更好的擴(kuò)展性。
此外,Runnable還可以用于“資源的共享”。即,多個(gè)線程都是基于某一個(gè)Runnable對(duì)象建立的,它們會(huì)共享Runnable對(duì)象上的資源。
通常,建議通過“Runnable”實(shí)現(xiàn)多線程!
Thread和Runnable的多線程示例
下面通過示例更好的理解Thread和Runnable,借鑒網(wǎng)上一個(gè)例子比較具有說服性的例子。
//ThreadTest.java 源碼
class MyThread extends Thread{
??? private int ticket=10;
??? public void run(){
???????? for(inti=0;i<20;i++){
???????????? if(this.ticket>0){
???????????????? System.out.println(this.getName()+" 賣票:ticket"+this.ticket--);
????????????? }
?????? }
??? }
};
public class ThreadTest {
??? public static void main(String[] args) {
??? //啟動(dòng)3個(gè)線程t1,t2,t3;每個(gè)線程各賣10張票!
??? MyThread t1=newMyThread();
??? MyThread t2=newMyThread();
??? MyThread t3=newMyThread();
??? t1.start();20t2.start();21t3.start();
??? }
}
運(yùn)行結(jié)果:
Thread-0賣票:ticket10
Thread-1賣票:ticket10
Thread-2賣票:ticket10
Thread-1賣票:ticket9
Thread-0賣票:ticket9
Thread-1賣票:ticket8
Thread-2賣票:ticket9
Thread-1賣票:ticket7
Thread-0賣票:ticket8
Thread-1賣票:ticket6
Thread-2賣票:ticket8
Thread-1賣票:ticket5
Thread-0賣票:ticket7
Thread-1賣票:ticket4
Thread-2賣票:ticket7
Thread-1賣票:ticket3
Thread-0賣票:ticket6
Thread-1賣票:ticket2
Thread-2賣票:ticket6
Thread-2賣票:ticket5
Thread-2賣票:ticket4
Thread-1賣票:ticket1
Thread-0賣票:ticket5
Thread-2賣票:ticket3
Thread-0賣票:ticket4
Thread-2賣票:ticket2
Thread-0賣票:ticket3
Thread-2賣票:ticket1
Thread-0賣票:ticket2
Thread-0 賣票:ticket1
結(jié)果說明:
(01) MyThread繼承于Thread,它是自定義個(gè)線程。每個(gè)MyThread都會(huì)賣出10張票。
(02) 主線程main創(chuàng)建并啟動(dòng)3個(gè)MyThread子線程。每個(gè)子線程都各自賣出了10張票。
下面,我們對(duì)上面的程序進(jìn)行修改。通過Runnable實(shí)現(xiàn)一個(gè)接口,從而實(shí)現(xiàn)多線程。
//RunnableTest.java 源碼
class MyThread implements Runnable{
???? private int ticket=10;
???? public void run(){
???????? for(inti=0;i<20;i++){
????????????? if(this.ticket>0){
????????????????? System.out.println(Thread.currentThread().getName()+" 賣票:ticket"+this.ticket--);
?????????????? }
???????? }
???? }
};
public class RunnableTest {
???? public static void main(String[] args) {
???? MyThread mt=newMyThread();
???? //啟動(dòng)3個(gè)線程t1,t2,t3(它們共用一個(gè)Runnable對(duì)象),這3個(gè)線程一共賣10張票!
???? Thread t1=newThread(mt);
????? Thread t2=newThread(mt);
????? Thread t3=newThread(mt);
?????? t1.start();
?????? t2.start();
?????? t3.start();
?????? }
}
運(yùn)行結(jié)果:
Thread-0賣票:ticket10
Thread-2賣票:ticket8
Thread-1賣票:ticket9
Thread-2賣票:ticket6
Thread-0賣票:ticket7
Thread-2賣票:ticket4
Thread-1賣票:ticket5
Thread-2賣票:ticket2
Thread-0賣票:ticket3
Thread-1 賣票:ticket1
結(jié)果說明:
(01) 和上面“MyThread繼承于Thread”不同;這里的MyThread實(shí)現(xiàn)了Thread接口。
(02) 主線程main創(chuàng)建并啟動(dòng)3個(gè)子線程,而且這3個(gè)子線程都是基于“mt這個(gè)Runnable對(duì)象”而創(chuàng)建的。運(yùn)行結(jié)果是這3個(gè)子線程一共賣出了10張票。這說明它們是共享了MyThread接口的。