Java 線程之Thread,Runnable,Callable

Java 中實現多線程有三種方法:

(1): 繼承Thread 類,覆蓋run() 方法。在run() 方法中不可拋異常,無返回值。

(2): 實現 Runnable接口,實現 其中的run() 方法。同(1) run() 不可拋異常,且無返回值。

(3):實現Callable<T>接口,接口中要覆蓋的方法是 public <T> call(),該方法可以拋出拋異常, 且可帶返回值.

對於前兩種方法,小白們應該知曉具體用法,引用其他帖子,闡述的淺顯易懂,具體如下:

一種是繼承Thread類,一種是實現Runnable接口;Thread類是在java.lang包中定義的。一個類只要繼承了Thread類同時覆寫了本類中的run()方法就可以實現多線程操作了,但是一個類只能繼承一個父類,這是此方法的侷限,

  下面看例子:

package org.thread.demo;

class MyThread extends Thread{

private String name;

public MyThread(String name) {

super();

this.name = name;

}

public void run(){

for(int i=0;i<10;i++){

System.out.println("線程開始:"+this.name+",i="+i);

}

}

}

package org.thread.demo;

public class ThreadDemo01 {

public static void main(String[] args) {

MyThread mt1=new MyThread("線程a");

MyThread mt2=new MyThread("線程b");

mt1.run();

mt2.run();

}

}

  但是,此時結果很有規律,先第一個對象執行,然後第二個對象執行,並沒有相互運行。在JDK的文檔中可以發現,一旦調用start()方法,則會通過JVM找到run()方法。下面啓動

start()方法啓動線程:

package org.thread.demo;

public class ThreadDemo01 {

public static void main(String[] args) {

MyThread mt1=new MyThread("線程a");

MyThread mt2=new MyThread("線程b");

mt1.start();

mt2.start();

}

};這樣程序可以正常完成交互式運行。那麼爲啥非要使用start();方法啓動多線程呢?

  在JDK的安裝路徑下,src.zip是全部的java源程序,通過此代碼找到Thread中的start()方法的定義,可以發現此方法中使用了private native void start0();其中native關鍵字表示可以調用操作系統的底層函數,那麼這樣的技術稱爲JNI技術(Java NativeInterface)

  ·Runnable接口

  在實際開發中一個多線程的操作很少使用Thread類,而是通過Runnable接口完成。

public interface Runnable{

public void run();

}

  例子:

package org.runnable.demo;

class MyThread implements Runnable{

private String name;

public MyThread(String name) {

this.name = name;

}

public void run(){

for(int i=0;i<100;i++){

System.out.println("線程開始:"+this.name+",i="+i);

}

}

};

 

  但是在使用Runnable定義的子類中沒有start()方法,只有Thread類中才有。此時觀察Thread類,有一個構造方法:publicThread(Runnable targer)此構造方法接受Runnable的子類實例,也就是說可以通過Thread類來啓動Runnable實現的多線程。(start()可以協調系統的資源):

package org.runnable.demo;

import org.runnable.demo.MyThread;

public class ThreadDemo01 {

public static void main(String[] args) {

MyThread mt1=new MyThread("線程a");

MyThread mt2=new MyThread("線程b");

new Thread(mt1).start();

new Thread(mt2).start();

}

}

  · 兩種實現方式的區別和聯繫:

  在程序開發中只要是多線程肯定永遠以實現Runnable接口爲主,因爲實現Runnable接口相比

  繼承Thread類有如下好處:

->避免點繼承的侷限,一個類可以繼承多個接口。

->適合於資源的共享

  以賣票程序爲例,通過Thread類完成:

package org.demo.dff;

class MyThread extends Thread{

private int ticket=10;

public void run(){

for(int i=0;i<20;i++){

if(this.ticket>0){

System.out.println("賣票:ticket"+this.ticket--);

}

}

}

};

  下面通過三個線程對象,同時賣票:

package org.demo.dff;

public class ThreadTicket {

public static void main(String[] args) {

MyThread mt1=new MyThread();

MyThread mt2=new MyThread();

MyThread mt3=new MyThread();

mt1.start();//每個線程都各賣了10張,共賣了30張票

mt2.start();//但實際只有10張票,每個線程都賣自己的票

mt3.start();//沒有達到資源共享

}

}

  如果用Runnable就可以實現資源共享,下面看例子:

package org.demo.runnable;

class MyThread implements Runnable{

private int ticket=10;

public void run(){

for(int i=0;i<20;i++){

if(this.ticket>0){

System.out.println("賣票:ticket"+this.ticket--);

}

}

}

}

package org.demo.runnable;

public class RunnableTicket {

public static void main(String[] args) {

MyThread mt=new MyThread();

new Thread(mt).start();//同一個mt,但是在Thread中就不可以,如果用同一

new Thread(mt).start();//個實例化對象mt,就會出現異常

new Thread(mt).start();

}

};

  雖然現在程序中有三個線程,但是一共賣了10張票,也就是說使用Runnable實現多線程可以達到資源共享目的。

Runnable接口和Thread之間的聯繫:

public class Thread extends Object implements Runnable

  發現Thread類也是Runnable接口的子類。

第三種如何運行呢  Callable接口在util.concurrent包中,由線程池提交 import java.util.concurrent.*; ExecutorService e = Executors.newFixedThreadPool(10); 參數表示最多可以運行幾個線程 e.submit(); 這個裏面參數傳 實現Callable接口那個類的對象

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章