1. 程式人生 > >java多執行緒-初探(二)

java多執行緒-初探(二)

java多執行緒-初探(一)

 

常見的執行緒函式

sleep

當前執行緒暫停

join

加入到當前執行緒中

setPriority

執行緒優先順序

yield

臨時暫停

setDaemon

守護執行緒

 

sleep

當前執行緒休眠具體的毫秒值後喚醒。

InterruptedException異常:當執行緒休眠時異常中斷將丟擲此異常。

 

class Main

package com.thread.two;

public class Main {

    /**
     * 吃飯
     * @param args
     */
    public static void main(String[] args) {
        new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("執行前毫秒數:" + beginTime);
                // 執行sleep函式。當前呼叫的執行緒休眠固定毫秒數
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("執行結束毫秒數:" + endTime);
                System.out.println("執行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();
    }
}

 

執行結果

 

join

將當前執行緒加入到主執行緒。當前執行緒執行完成,主執行緒才能接著往下執行。

主執行緒:執行main函式時,整個程序是一個主執行緒,主執行緒從上往下依次執行,遇到Thread類開啟多執行緒時,執行緒類獨立執行,不影響主執行緒的執行步驟,就是說子執行緒獨立執行,主執行緒還是往下繼續執行。但是當子執行緒呼叫join函式加入到主執行緒當中,則主執行緒必須等子執行緒執行完成才能往下繼續執行。

 

class Main

package com.thread.two;

public class Main {

    /**
     * 吃飯
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("執行前毫秒數:" + beginTime);
                // 執行sleep函式。當前呼叫的執行緒休眠固定毫秒數
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("執行結束毫秒數:" + endTime);
                System.out.println("執行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        // join
        System.out.println("我是主執行緒-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每個一秒列印一次:我是子執行緒
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子執行緒");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
//        thread.join();
        System.out.println("我是主執行緒-end");
    }
}

註釋thread.join();的執行結果

開啟thread.join();的執行結果

 

setPriority

設定執行緒的優先順序,取值範圍:1-10

執行緒優先順序越高,該執行緒越容易獲得cpu資源,從而執行自身執行緒,相較於其他低優先順序的執行緒優先執行。

 

class Main

package com.thread.two;

public class Main {

    /**
     * 吃飯
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("執行前毫秒數:" + beginTime);
                // 執行sleep函式。當前呼叫的執行緒休眠固定毫秒數
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("執行結束毫秒數:" + endTime);
                System.out.println("執行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        /*// join
        System.out.println("我是主執行緒-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每個一秒列印一次:我是子執行緒
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子執行緒");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是主執行緒-end");*/

        // setPriority執行緒優先順序
        // 執行緒1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是執行緒----1");
                }
            }
        };
        // 執行緒二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    System.out.println("我是執行緒----2");
                }
            }
        };
        // 設定優先順序 取值:1-10
        thread1.setPriority(Thread.MIN_PRIORITY);// 最小
        thread2.setPriority(Thread.MAX_PRIORITY);// 最大
        thread1.start();
        thread2.start();
    }
}

資料量小無法體現,這裡只截部分執行結果

 

yield

臨時暫停當前執行緒,使得其餘執行緒能更容易獲得cpu資源執行。

 

class Main

package com.thread.two;

public class Main {

    /**
     * 吃飯
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("執行前毫秒數:" + beginTime);
                // 執行sleep函式。當前呼叫的執行緒休眠固定毫秒數
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("執行結束毫秒數:" + endTime);
                System.out.println("執行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        /*// join
        System.out.println("我是主執行緒-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每個一秒列印一次:我是子執行緒
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子執行緒");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是主執行緒-end");*/

        /*// setPriority執行緒優先順序
        // 執行緒1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是執行緒----1");
                }
            }
        };
        // 執行緒二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    System.out.println("我是執行緒----2");
                }
            }
        };
        // 設定優先順序 取值:1-10
        thread1.setPriority(Thread.MIN_PRIORITY);// 最小
        thread2.setPriority(Thread.MAX_PRIORITY);// 最大
        thread1.start();
        thread2.start();*/

        // yield 臨時暫停
        // 執行緒1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是執行緒----1");
                }
            }
        };
        // 執行緒二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    // 執行一半臨時暫停
                    if (i == 50) Thread.yield();
                    System.out.println("我是執行緒----2");
                }
            }
        };
        thread1.start();
        thread2.start();
    }
}

執行結果

 

setDaemon

守護執行緒,當一個程序裡全部都是守護執行緒時,結束當前整個程序。

守護執行緒一般用作業務附加的輔助功能,如程序執行的記錄日誌、檢測當前使用者狀態。

 

class Main

package com.thread.two;

public class Main {

    /**
     * 吃飯
     * @param args
     */
    public static void main(String[] args) {
        /*new Thread(){
            public void run(){
                long beginTime = System.currentTimeMillis();
                System.out.println("執行前毫秒數:" + beginTime);
                // 執行sleep函式。當前呼叫的執行緒休眠固定毫秒數
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("執行結束毫秒數:" + endTime);
                System.out.println("執行花了:" + (endTime - beginTime)/1000 +"秒");
            }
        }.start();*/

        /*// join
        System.out.println("我是主執行緒-begin");
        Thread thread = new Thread() {
            public void run(){
                // 每個一秒列印一次:我是子執行緒
                for(int i = 0 ; i < 3 ; i++){
                    System.out.println("我是子執行緒");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我是主執行緒-end");*/

        /*// setPriority執行緒優先順序
        // 執行緒1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是執行緒----1");
                }
            }
        };
        // 執行緒二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    System.out.println("我是執行緒----2");
                }
            }
        };
        // 設定優先順序 取值:1-10
        thread1.setPriority(Thread.MIN_PRIORITY);// 最小
        thread2.setPriority(Thread.MAX_PRIORITY);// 最大
        thread1.start();
        thread2.start();*/

        /*// yield 臨時暫停
        // 執行緒1
        Thread thread1 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++){
                    System.out.println("我是執行緒----1");
                }
            }
        };
        // 執行緒二
        Thread thread2 = new Thread(){
            public void run(){
                for (int i = 0 ; i < 100 ; i++) {
                    // 執行一半臨時暫停
                    if (i == 50) Thread.yield();
                    System.out.println("我是執行緒----2");
                }
            }
        };
        thread1.start();
        thread2.start();*/

        // setDaemon 守護執行緒
        System.out.println("主執行緒開始");
        Thread thread = new Thread(){
            public void run(){
                while (true){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("我是守護執行緒!");
                }
            }
        };
        thread.setDaemon(true);
        thread.start();
        System.out.println("主執行緒結束");
    }
}

執行結果

主執行緒結束,守護執行緒也相應結束,不會繼續執行。

當所有執行緒都是守護執行緒時,程序結束執行。

 

java多執行緒-初探(三)