Java中Runnable和Thread以及Callable的區別
在java中,其實java中實現 多執行緒有三種方法,一種是繼承Thread類;第二種是實現Runnable介面;第三種是實現Callable介面。
1,繼承Thread
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 Native Interface)
2,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類,有一個構造方法:public Thread(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類有如下好處:
1,避免點繼承的侷限,一個類可以繼承多個介面。
2,適合於資源的共享
Runnable介面和Thread之間的聯絡:
public class Thread extends Object implements Runnable
發現Thread類也是Runnable介面的子類。
3,Callable介面
Callable 和 Runnable 的使用方法大同小異, 區別在於:
1.Callable 使用 call() 方法, Runnable 使用 run() 方法
2.call() 可以返回值, 而 run()方法不能返回。
3.call() 可以丟擲受檢查的異常,比如ClassNotFoundException, 而run()不能丟擲受檢查的異常。
Callable示例如下:
Java程式碼
class TaskWithResult implements Callable<String> {
private int id;
public TaskWithResult(int id) {
this.id = id;
}
@Override
public String call() throws Exception {
return "result of TaskWithResult " + id;
}
}
public class CallableTest {
public static void main(String[] args) throws InterruptedException,
ExecutionException {
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Future<String>> results = new ArrayList<Future<String>>(); //Future 相當於是用來存放Executor執行的結果的一種容器
for (int i = 0; i < 10; i++) {
results.add(exec.submit(new TaskWithResult(i)));
}
for (Future<String> fs : results) {
if (fs.isDone()) {
System.out.println(fs.get());
} else {
System.out.println("Future result is not yet complete");
}
}
exec.shutdown();
}
}