1. 程式人生 > 其它 >Thread與Runnable區別

Thread與Runnable區別

Thread與Runnable區別

public class MyThread extends Thread { private int ticket = 5; @Override public void run() { while (true) { System.out.println("Thread ticket = " + ticket--); if (ticket < 0) { break; } } } } public class Test { public static void main(String[] args) { //Test Thread new MyThread().start(); new MyThread().start(); } } 執行結果如下: Thread ticket = 5 Thread ticket = 4 Thread ticket = 3 Thread ticket = 2 Thread ticket = 1 Thread ticket = 0 Thread ticket = 5 Thread ticket = 4 Thread ticket = 3 Thread ticket = 2 Thread ticket = 1 Thread ticket = 0

並不能實現資源共享,跟以前背的面試題答案一樣;但真的是這樣嗎?我們仔細看一下,程式碼中我們建立了兩個MyThread物件,每個物件都有自己的ticket成員變數,當然會多賣1倍。現在我們重新測試一下,請看測試程式碼:

public class Test {
    public static void main(String[] args) {

            //Test Thread

        MyThread t1 = new MyThread();
        new Thread(t1).start();
        new Thread(t1).start();
    }
}

測試結果如下:
Thread ticket = 5
Thread ticket = 4
Thread ticket = 3
Thread ticket = 2
Thread ticket = 1
Thread ticket = 0
Thread ticket = -1

可以看到這次我們只建立了一個MyThread物件,並沒出現賣兩倍票的情況,Thread也可以實現資源共享。

因為多執行緒訪問同一變數會有併發問題(需要加鎖),所以Thread正確的寫法如下:

public class MyThread extends Thread {

    private int ticket = 5;

    @Override
    public void run() {
        if (ticket > 0) {
            synchronized (this) {
                if (ticket > 0) {
                    while (true) {
                        System.out.println("Thread:" + Thread.currentThread().getName() + "--Thread ticket = " + ticket--);
                        if (ticket < 0) {
                            break;
                        }
                    }
                }
            }
        }
    }

}

public class Test {
    public static void main(String[] args) {

            //Test Thread

        MyThread t1 = new MyThread();
        new Thread(t1).start();
        new Thread(t1).start();
        new Thread(t1).start();
        new Thread(t1).start();
        new Thread(t1).start();
        new Thread(t1).start();
    }
}

執行結果如下:
Thread:Thread-1--Thread ticket = 5
Thread:Thread-1--Thread ticket = 4
Thread:Thread-1--Thread ticket = 3
Thread:Thread-1--Thread ticket = 2
Thread:Thread-1--Thread ticket = 1
Thread:Thread-1--Thread ticket = 0

現在看Thread和Runnable的原始碼:


public
class Thread implements Runnable {
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    private volatile String name;
    private int            priority;
    private Thread         threadQ;
    private long           eetop;
    
    

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

結論:其實Thread也就是實現了Runnable介面,提供了更多的方法而已。所以說Thread與Runnable並沒有什麼區別。如果硬要說有什麼區別的話,那就是類與介面的區別,繼承與實現的區別。另外最重要的是,學習的時候不要忘了思考哦!