java中thread和runnable的區別和聯絡
在java中可有兩種方式實現多執行緒,一種是繼承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;
- }
- publicvoid
- for(int i=0;i<10;i++){
- System.out.println("執行緒開始:"+this.name+",i="+i);
- }
- }
- }
- package org.thread.demo;
- publicclass ThreadDemo01 {
- publicstaticvoid 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;
- publicclass ThreadDemo01 {
- publicstaticvoid 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)
Runnable介面
在實際開發中一個多執行緒的操作很少使用Thread類,而是通過Runnable介面完成。
- publicinterface Runnable{
- publicvoid run();
- }
例子:
- package org.runnable.demo;
- class MyThread implements Runnable{
- private String name;
- public MyThread(String name) {
- this.name = name;
- }
- publicvoid 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;
- publicclass ThreadDemo01 {
- publicstaticvoid 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{
- privateint ticket=10;
- publicvoid run(){
- for(int i=0;i<20;i++){
- if(this.ticket>0){
- System.out.println("賣票:ticket"+this.ticket--);
- }
- }
- }
- };
下面通過三個執行緒物件,同時賣票:
- package org.demo.dff;
- publicclass ThreadTicket {
- publicstaticvoid 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{
- privateint ticket=10;
- publicvoid run(){
- for(int i=0;i<20;i++){
- if(this.ticket>0){
- System.out.println("賣票:ticket"+this.ticket--);
- }
- }
- }
- }
- package org.demo.runnable;
- publicclass RunnableTicket {
- publicstaticvoid 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介面的子類。
轉:http://blog.csdn.net/wwww1988600/article/details/7309070