001-創建多線程以及創建方式的比較

創建線程

繼承Thread

public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("mythread ouput" + i);
        }
    }
}
public class MultiThread {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        for (int i = 0; i < 100; i++) {
            System.out.println("main output " + i);
        }
    }
}

運行MultiThread類,得到如下結果:

main output 0
mythread ouput0
main output 1
mythread ouput1
mythread ouput2
main output 2
mythread ouput3
main output 3
mythread ouput4
main output 4
mythread ouput5
main output 5
mythread ouput6
main output 6
mythread ouput7

修改MultiThread類,將start()方法修改爲run()

public class MultiThread {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.run();
        for (int i = 0; i < 100; i++) {
            System.out.println("main output " + i);
        }
    }
}

運行MultiThread類,得到如下結果:

......
mythread ouput96
mythread ouput97
mythread ouput98
mythread ouput99
main output 0
main output 1
main output 2
main output 3
main output 4
......

由上面的運行結果可知:run()只是線程的一個普通方法,在主線程裏執行。start()啓動相應的線程,並執行該線程的run()。

實現Runnable

public class MyThread implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("mythread output" + i);
        }
    }
}
public class MultiClass {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyThread());
        thread.start();
        for (int i = 0; i < 100; i++) {
            System.out.println("main output" + i);
        }
    }
}

運行MultiClass main()得到如下結果:

......
main output8
main output9
main output10
main output11
......

通過賣票小案例,來理解兩種創建方式的區別

public class MyThead extends Thread {
    private int total = 10;
    private String name;

    public MyThead(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            if (this.total > 0) {
                System.out.println(this.name + "售出" + this.total--);
            } else {
                break;
            }
        }
    }
}

public class MultiClass {
    public static void main(String[] args) {
        MyThead t = new MyThead("線程1");
        MyThead t2 = new MyThead("線程2");
        MyThead t3 = new MyThead("線程3");

        t.start();
        t2.start();
        t3.start();
    }
}

運行MultiClass main()得到如下結果:

線程1售出10
線程1售出9
線程2售出10
線程1售出8
線程1售出7
線程1售出6
線程1售出5
線程3售出10
線程3售出9
線程3售出8
線程3售出7
線程3售出6
線程3售出5
線程3售出4
線程3售出3
線程3售出2
線程3售出1
線程2售出9
線程2售出8
線程2售出7
線程2售出6
線程1售出4
線程2售出5
線程1售出3
線程1售出2
線程1售出1
線程2售出4
線程2售出3
線程2售出2
線程2售出1
public class MyRunnableClass implements Runnable {
    private int total = 10;

    @Override
    public void run() {
        while (true) {
            if (this.total > 0) {
                System.out.println(Thread.currentThread().getName() + "售出" + this.total--);
            }else{
                break;
            }
        }
    }
}
public class MultiRunnableClass {
    public static void main(String[] args) {
        MyRunnableClass thread = new MyRunnableClass();
        Thread t = new Thread(thread,"線程1");
        Thread t2 = new Thread(thread,"線程2");
        Thread t3 = new Thread(thread,"線程3");
        t.start();t2.start();t3.start();
    }
}

運行MultiRunnableClass main()得到如下結果:

線程1售出10
線程3售出8
線程2售出9
線程3售出6
線程3售出4
線程1售出7
線程3售出3
線程2售出5
線程3售出1
線程1售出2

通過上面的運行結果可知:

  1. MultiRunnableClass main()中創建了三個線程 只有一份任務 三個線程共同完成一項任務
  2. MultiClass main()中創建三個線程的同時 創建了三個任務,每個線程獨立跑自己任務
  3. 如果將Runnable比作job,Thread比作worker,就不難理解了。由於Thread實現了Runnable接口,每次new 繼承了Thread的類,都會創建一個job和一個worker,然後worker在獨立的環境中執行job。而通過new Thread(target),傳入實現了Runnable的類,不管創建多少個worker,都只會有一個job,多個worker執行一個job。
  4. 至於選擇那種實現方式來創建線程,取決於具體的業務環境,相信通過上面的代碼,可以做到心中有數。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章