Java中Runnable和Thread的區別


概述

Runnable 是接口。

Thread 是類,且實現了Runnable接口。

Thread部分源碼

public class Thread
    implements Runnable
{
    private static class Caches
    {
 
        static final ConcurrentMap subclassAudits = new ConcurrentHashMap();
        static final ReferenceQueue subclassAuditsQueue = new ReferenceQueue();

在使用Runnable定義的子類中沒有start()方法,只有Thread類中才有。

 

1 public interface Runnable
2 {
3  
4     public abstract void run();
5 }

Thread類,有一個構造方法:public Thread(Runnable targer)

 

 1     public Thread(Runnable runnable)
 2     {
 3         daemon = false;
 4         stillborn = false;
 5         threadLocals = null;
 6         inheritableThreadLocals = null;
 7         threadStatus = 0;
 8         blockerLock = new Object();
 9         init(null, runnable, (new StringBuilder()).append("Thread-").append(nextThreadNum()).toString(), 0L);
10     }

此構造方法接受Runnable的子類實例,也就是說可以通過Thread類來啟動Runnable實現的多線程。

使用情況

在程序開發中只要是多線程肯定永遠以實現Runnable接口為主。

實現Runnable接口相比繼承Thread類有如下好處:
1、避免繼承的局限,一個類可以繼承多個接口。
2、適合於資源的共享。

實例

以賣票為例,總共只有10張動車票了,全國3個窗口在賣。

 

 

繼承Thread類的方法

 1 package multithreading;
 2  
 3 public class MyThreadWithExtends extends Thread {
 4  
 5     private int tickets = 10;
 6  
 7     @Override
 8     public void run() {
 9  
10         for (int i = 0; i <= 100; i++) {
11             if(tickets>0){
12                 System.out.println(Thread.currentThread().getName()+"--賣出票:" + tickets--);
13             }
14         }
15     }
16     
17     
18     public static void main(String[] args) {
19         MyThreadWithExtends thread1 = new MyThreadWithExtends();
20         MyThreadWithExtends thread2 = new MyThreadWithExtends();
21         MyThreadWithExtends thread3 = new MyThreadWithExtends();
22  
23         thread1.start();
24         thread2.start();
25         thread3.start();
26         
27         //每個線程都獨立,不共享資源,每個線程都賣出了10張票,總共賣出了30張。如果真賣票,就有問題了。
28     }
29  
30 }

運行結果:

Thread-0--賣出票:10

Thread-2--賣出票:10

Thread-1--賣出票:10

Thread-2--賣出票:9

Thread-0--賣出票:9

Thread-2--賣出票:8

Thread-1--賣出票:9

Thread-2--賣出票:7

Thread-0--賣出票:8

Thread-2--賣出票:6

Thread-2--賣出票:5

Thread-2--賣出票:4

Thread-1--賣出票:8

Thread-2--賣出票:3

Thread-0--賣出票:7

Thread-2--賣出票:2

Thread-2--賣出票:1

Thread-1--賣出票:7

Thread-0--賣出票:6

Thread-1--賣出票:6

Thread-0--賣出票:5

Thread-0--賣出票:4

Thread-1--賣出票:5

Thread-0--賣出票:3

Thread-1--賣出票:4

Thread-1--賣出票:3

Thread-1--賣出票:2

Thread-0--賣出票:2

Thread-1--賣出票:1

Thread-0--賣出票:1

每個線程都獨立,不共享資源,每個線程都賣出了10張票,總共賣出了30張。如果真賣票,就有問題了。

實現Runnable接口方式

 1 package multithreading;
 2  
 3 public class MyThreadWithImplements implements Runnable {
 4  
 5     private int tickets = 10;
 6  
 7     @Override
 8     public void run() {
 9  
10         for (int i = 0; i <= 100; i++) {
11             if(tickets>0){
12                 System.out.println(Thread.currentThread().getName()+"--賣出票:" + tickets--);
13             }
14         }
15     }
16     
17     
18     public static void main(String[] args) {
19         MyThreadWithImplements myRunnable = new MyThreadWithImplements();
20         Thread thread1 = new Thread(myRunnable, "窗口一");
21         Thread thread2 = new Thread(myRunnable, "窗口二");
22         Thread thread3 = new Thread(myRunnable, "窗口三");
23  
24         thread1.start();
25         thread2.start();
26         thread3.start();
27     }
28  
29 }

運行結果:

 

窗口二--賣出票:10
窗口三--賣出票:9
窗口一--賣出票:8
窗口三--賣出票:6
窗口三--賣出票:4
窗口三--賣出票:3
窗口三--賣出票:2
窗口三--賣出票:1
窗口二--賣出票:7
窗口一--賣出票:5

 

每個線程共享了對象myRunnable的資源,賣出的總票數是對的,但是順序是亂的,怎么辦?

見:同步關鍵詞synchronized

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM