程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Runnable和Thread,RunnableThread

Runnable和Thread,RunnableThread

編輯:JAVA綜合教程

Runnable和Thread,RunnableThread


1、Runnable是一個接口,當實現該接口時需要復用run方法,在run方法中實現自己的邏輯。

2、Thread是一個類,它其實實現了Runnable方法,也就是說當你通過new 一個Thread得到一個線程實例時,其實就是重寫runnable裡面的run方法。當你通過實現runnable創建一個線程實例時,你需要將runnable實例傳遞一個thread,然後通過.start開啟線程。

3、Runnable可以線程同步,但是Thread不可以線程同步,例如一共有10張票,你可以開啟3個線程同時賣票,如果你通過繼承thread創建一個賣票的類,實例化3個線程會各自賣10張票,一共就賣出了30張,當時你通過實現runnable所創建的賣票類,只需要實例化一個對象,將一個對象傳遞給三個thread,然後開啟三個線程,你會發現三個線程一共只會賣10張。

4、以下是賣票的代碼

package com.shine.test;
/**
 * 通過繼承thread的賣票類
 * @author ASUS
 *
 */
public class MyThread extends Thread {
    
    public   int ticks = 10;
    boolean flag = true;
    public MyThread(String string) {
        this.setName(string);
    }

    @Override
    public void run() {
        while(flag){
            saleTick();
        }
    }

    public void saleTick() {
        if(ticks > 0){
            ticks--;
            
            System.out.println(Thread.currentThread().getName()+":"+ticks);
        }else{
            flag = false;
        }
    }

}
package com.shine.test;
/**
 * 通過實現Runnable的賣票類
 * @author ASUS
 *
 */
public class MyRunnable implements Runnable {

    public  int ticks = 10;
    boolean flag = true;
    @Override
    public void run() {
        while(flag){
            saleTick();
        }

    }
    public void saleTick() {
        if(ticks > 0){
            ticks--;
            System.out.println(Thread.currentThread().getName()+":"+ticks);
        }else{
            flag = false;
        }
    }
}
package com.shine.test;

public class Test {

    /**測試
     * @param args
     */
    public static void main(String[] args) {
        MyThread myThread1 = new MyThread("thread1");
        MyThread myThread2 = new MyThread("thread2");
        MyThread myThread3 = new MyThread("thread3");
        myThread1.start();
        myThread2.start();
        myThread3.start();
        
        MyRunnable myRunnable = new MyRunnable();
        Thread runnable1 = new Thread(myRunnable);
        Thread runnable2 = new Thread(myRunnable);
        Thread runnable3 = new Thread(myRunnable);
        runnable1.setName("runnable1");
        runnable2.setName("runnable2");
        runnable3.setName("runnable3");
        runnable1.start();
        runnable2.start();
        runnable3.start();
        
    }

}

結果:
thread1:9 thread1:8 thread1:7 thread1:6 thread1:5 thread1:4 thread1:3 thread1:2 thread1:1 thread1:0 thread2:9 thread2:8 thread2:7 thread2:6 thread2:5 thread2:4 thread2:3 thread2:2 thread2:1 thread2:0 thread3:9 runnable1:9 runnable1:8 runnable1:7 runnable1:6 runnable1:5 runnable1:4 runnable1:3 runnable1:2 runnable1:1 runnable1:0 thread3:8 thread3:7 thread3:6 thread3:5 thread3:4 thread3:3 thread3:2 thread3:1 thread3:0

由於系統自身的線程選擇問題 全部由runnable1完成了賣票 但總共只賣出了10張 thread的則賣出了30張

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved