在java中朵你,其實(shí)java中實(shí)現(xiàn) 多線程有三種方法,一種是繼承Thread類揣非;第二種是實(shí)現(xiàn)Runnable接口抡医;第三種是實(shí)現(xiàn)Callable接口。
1早敬,繼承Thread
Thread類是在java.lang包中定義的忌傻。一個(gè)類只要繼承了Thread類同時(shí)覆寫了本類中的run()方法就可以實(shí)現(xiàn)多線程操作了,但是一個(gè)類只能繼承一個(gè)父類搞监,這是此方法的局限水孩。
下面看例子:
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();
}
}
但是,此時(shí)結(jié)果很有規(guī)律琐驴,先第一個(gè)對(duì)象執(zhí)行俘种,然后第二個(gè)對(duì)象執(zhí)行,并沒(méi)有相互運(yùn)行绝淡。在JDK的文檔中可以發(fā)現(xiàn)宙刘,一旦調(diào)用start()方法,則會(huì)通過(guò)JVM找到run()方法牢酵。下面啟動(dòng)start()方法啟動(dòng)線程:
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();
}
};
這樣程序可以正常完成交互式運(yùn)行悬包。那么為啥非要使用start();方法啟動(dòng)多線程呢?
在JDK的安裝路徑下馍乙,src.zip是全部的java源程序布近,通過(guò)此代碼找到Thread中的start()方法的定義,可以發(fā)現(xiàn)此方法中使用了private native void start0();其中native關(guān)鍵字表示可以調(diào)用操作系統(tǒng)的底層函數(shù)潘拨,那么這樣的技術(shù)成為JNI技術(shù)(java Native Interface)
2,Runnable接口
在實(shí)際開發(fā)中一個(gè)多線程的操作很少使用Thread類饶号,而是通過(guò)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定義的子類中沒(méi)有start()方法铁追,只有Thread類中才有。此時(shí)觀察Thread類茫船,有一個(gè)構(gòu)造方法:public Thread(Runnable targer)此構(gòu)造方法接受Runnable的子類實(shí)例琅束,也就是說(shuō)可以通過(guò)Thread類來(lái)啟動(dòng)Runnable實(shí)現(xiàn)的多線程扭屁。(start()可以協(xié)調(diào)系統(tǒng)的資源):
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();
}
}
兩種實(shí)現(xiàn)方式的區(qū)別和聯(lián)系:
在程序開發(fā)中只要是多線程肯定永遠(yuǎn)以實(shí)現(xiàn)Runnable接口為主,因?yàn)閷?shí)現(xiàn)Runnable接口相比繼承Thread類有如下好處:
1,避免點(diǎn)繼承的局限涩禀,一個(gè)類可以繼承多個(gè)接口料滥。
2,適合于資源的共享
Runnable接口和Thread之間的聯(lián)系:
public class Thread extends Object implements Runnable
發(fā)現(xiàn)Thread類也是Runnable接口的子類。
3艾船,Callable接口
Callable 和 Runnable 的使用方法大同小異葵腹, 區(qū)別在于:
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();
//Future 相當(dāng)于是用來(lái)存放Executor執(zhí)行的結(jié)果的一種容器
ArrayList<Future<String>> results = new ArrayList<Future<String>>();
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();
}
}