自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java中Runnable和Thread的區(qū)別

開發(fā) 后端
在java中可有兩種方式實(shí)現(xiàn)多線程,一種是繼承Thread類,一種是實(shí)現(xiàn)Runnable接口;Thread類是在java.lang包中定義的。一個(gè)類只要繼承了Thread類同時(shí)覆寫了本類中的run()方法就可以實(shí)現(xiàn)多線程操作了,但是一個(gè)類只能繼承一個(gè)父類,這是此方法的局限。

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

下面看例子:

  1.   package org.thread.demo;  
  2.   class MyThread extends Thread{  
  3.   private String name;  
  4.   public MyThread(String name) {  
  5.   super();  
  6.   this.name = name;  
  7.   }  
  8.   public void run(){  
  9.   for(int i=0;i<10;i++){  
  10.   System.out.println("線程開始:"+this.name+",i="+i);  
  11.   }  
  12.   }  
  13.   }  
  14.   package org.thread.demo;  
  15.   public class ThreadDemo01 {  
  16.   public static void main(String[] args) {  
  17.   MyThread mt1=new MyThread("線程a");  
  18.   MyThread mt2=new MyThread("線程b");  
  19.   mt1.run();  
  20.   mt2.run();  
  21.   }  
  22.   } 

但是,此時(shí)結(jié)果很有規(guī)律,先***個(gè)對象執(zhí)行,然后第二個(gè)對象執(zhí)行,并沒有相互運(yùn)行。在JDK的文檔中可以發(fā)現(xiàn),一旦調(diào)用start()方法,則會通過JVM找到run()方法。下面啟動start()方法啟動線程:

  1.   package org.thread.demo;  
  2.   public class ThreadDemo01 {  
  3.   public static void main(String[] args) {  
  4.   MyThread mt1=new MyThread("線程a");  
  5.   MyThread mt2=new MyThread("線程b");  
  6.   mt1.start();  
  7.   mt2.start();  
  8.   }  
  9.   }; 

這樣程序可以正常完成交互式運(yùn)行。那么為啥非要使用start();方法啟動多線程呢?

在JDK的安裝路徑下,src.zip是全部的java源程序,通過此代碼找到Thread中的start()方法的定義,可以發(fā)現(xiàn)此方法中使用了private native void start0();其中native關(guān)鍵字表示可以調(diào)用操作系統(tǒng)的底層函數(shù),那么這樣的技術(shù)成為JNI技術(shù)(java Native Interface)

Runnable接口

在實(shí)際開發(fā)中一個(gè)多線程的操作很少使用Thread類,而是通過Runnable接口完成。

  1. public interface Runnable{  
  2. public void run();  

例子:

  1. package org.runnable.demo;  
  2.   class MyThread implements Runnable{  
  3.   private String name;  
  4.   public MyThread(String name) {  
  5.   this.name = name;  
  6.   }
  7.   public void run(){  
  8.   for(int i=0;i<100;i++){  
  9.   System.out.println("線程開始:"+this.name+",i="+i);  
  10.   }  
  11.   }  
  12.   }; 

但是在使用Runnable定義的子類中沒有start()方法,只有Thread類中才有。此時(shí)觀察Thread類,有一個(gè)構(gòu)造方法:public Thread(Runnable targer)此構(gòu)造方法接受Runnable的子類實(shí)例,也就是說可以通過Thread類來啟動Runnable實(shí)現(xiàn)的多線程。(start()可以協(xié)調(diào)系統(tǒng)的資源):

  1.   package org.runnable.demo;  
  2.   import org.runnable.demo.MyThread;  
  3.   public class ThreadDemo01 {  
  4.   public static void main(String[] args) {  
  5.   MyThread mt1=new MyThread("線程a");  
  6.   MyThread mt2=new MyThread("線程b");  
  7.   new Thread(mt1).start();  
  8.   new Thread(mt2).start();  
  9.   }  
  10.   } 

兩種實(shí)現(xiàn)方式的區(qū)別和聯(lián)系:

在程序開發(fā)中只要是多線程肯定永遠(yuǎn)以實(shí)現(xiàn)Runnable接口為主,因?yàn)閷?shí)現(xiàn)Runnable接口相比繼承Thread類有如下好處:

  • 避免點(diǎn)繼承的局限,一個(gè)類可以繼承多個(gè)接口。
  • 適合于資源的共享

以賣票程序?yàn)槔?,通過Thread類完成:

  1.   package org.demo.dff;  
  2.   class MyThread extends Thread{  
  3.   private int ticket=10;  
  4.   public void run(){  
  5.   for(int i=0;i<20;i++){  
  6.   if(this.ticket>0){  
  7.   System.out.println("賣票:ticket"+this.ticket--);  
  8.   }  
  9.   }  
  10.   }  
  11.   }; 

下面通過三個(gè)線程對象,同時(shí)賣票:

  1.   package org.demo.dff;  
  2.   public class ThreadTicket {  
  3.   public static void main(String[] args) {  
  4.   MyThread mt1=new MyThread();  
  5.   MyThread mt2=new MyThread();  
  6.   MyThread mt3=new MyThread();  
  7.   mt1.start();//每個(gè)線程都各賣了10張,共賣了30張票  
  8.   mt2.start();//但實(shí)際只有10張票,每個(gè)線程都賣自己的票  
  9.   mt3.start();//沒有達(dá)到資源共享  
  10.   }  
  11.   } 

如果用Runnable就可以實(shí)現(xiàn)資源共享,下面看例子:

  1.   package org.demo.runnable;  
  2.   class MyThread implements Runnable{  
  3.   private int ticket=10;  
  4.   public void run(){  
  5.   for(int i=0;i<20;i++){  
  6.   if(this.ticket>0){  
  7.   System.out.println("賣票:ticket"+this.ticket--);  
  8.   }  
  9.   }  
  10.   }  
  11.   }  
  12.   package org.demo.runnable;  
  13.   public class RunnableTicket {  
  14.   public static void main(String[] args) {  
  15.   MyThread mt=new MyThread();  
  16.   new Thread(mt).start();//同一個(gè)mt,但是在Thread中就不可以,如果用同一  
  17.   new Thread(mt).start();//個(gè)實(shí)例化對象mt,就會出現(xiàn)異常  
  18.   new Thread(mt).start();  
  19.   }  
  20.   }; 

雖然現(xiàn)在程序中有三個(gè)線程,但是一共賣了10張票,也就是說使用Runnable實(shí)現(xiàn)多線程可以達(dá)到資源共享目的。

Runnable接口和Thread之間的聯(lián)系:

public class Thread extends Object implements Runnable

發(fā)現(xiàn)Thread類也是Runnable接口的子類。

原文鏈接:http://blog.csdn.net/wwww1988600/article/details/7309070

【編輯推薦】

  1. Java對存儲過程的調(diào)用方法
  2. Java初學(xué)者都必須理解的六大問題
  3. 深入Java關(guān)鍵字null
  4. Java 5線程池使用
  5. 淺析Java抽象類和接口的比較
責(zé)任編輯:林師授 來源: wwww1988600的博客
相關(guān)推薦

2023-10-12 08:25:18

Javaequals內(nèi)存

2022-07-26 07:51:40

ThreadRunnableFuture

2009-06-19 16:46:18

IntegerJava

2021-06-10 18:59:41

Java編程語言

2011-05-26 14:49:53

ArrayListLinkedList

2011-07-21 17:02:48

JAVA模式框架

2021-11-10 15:18:16

JavaGo命令

2023-07-03 08:10:51

2011-07-10 14:07:59

JAVA

2024-08-12 15:23:43

LangChain

2021-08-04 08:33:59

TypeScriptConst Readonly

2013-05-21 15:03:23

MariaDB

2010-09-01 15:11:09

linkimportCSS

2015-08-10 10:58:53

dompropertyattribute

2010-09-08 09:33:09

CSSlink@import

2011-04-20 09:07:44

Ubuntuuseraddadduser

2010-08-30 10:32:38

SPANDIV

2010-03-18 14:02:59

Java Runnab

2009-06-29 17:54:10

Java多線程Thread類創(chuàng)建線程

2016-12-06 10:30:39

JavaScriptWriteWriteln
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號