Java_基礎—設置休眠/守護/插隊/禮讓/優先級線程

一、休眠線程(Sleep)

Thread.sleep(毫秒), 控制當前線程休眠若干毫秒
1秒= 1000毫秒
1秒 = 1000 * 1000 * 1000納秒 1000000000

package com.soar.threadmethod;

public class Demo3_Sleep {

    public static void main(String[] args) {
        //demo1();
        new Thread(){
            public void run(){
                for(int i = 0; i<10; i++){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    }
                    System.out.println(getName()+"   aaaaa");
                }
            }
        }.start();

        new Thread(){
            public void run(){
                for(int i = 0; i<10; i++){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    }
                    System.out.println(getName()+"   bb");
                }
            }
        }.start();
    }

    public static void demo1() throws InterruptedException {
        for (int i = 20; i >=0; i--) {
            Thread.sleep(1000);
            System.out.println("倒計時第:"+i+"秒");
        }
    }

}

二、守護線程(setDaemon)

setDaemon(), 設置一個線程爲守護線程, 該線程不會單獨執行, 當其他非守護線程都執行結束後, 自動退出

package com.soar.threadmethod;

public class Demo4_Daemon {
    /*
     * 守護線程
     */
    public static void main(String[] args) {
        Thread t1= new Thread(){
            public void run(){
                for(int i=0; i<2; i++){
                    System.out.println(getName()+"   aaaaaa");
                }
            }
        };

        Thread t2= new Thread(){
            public void run(){
                for(int i=0; i<50; i++){
                    System.out.println(getName()+"   bb");
                }
            }
        };

        t2.setDaemon(true);         //但傳入true就是意味着設置爲守護線程
        t1.start();
        t2.start();
    }

}

Console:

Thread-0   aaaaaa
Thread-0   aaaaaa
Thread-1   bb
Thread-1   bb
Thread-1   bb

當t1執行完結束後,t2不會立馬退出,有一段緩衝的時間。

三、加入(插隊)線程(join)

join(), 當前線程暫停, 等待指定的線程執行結束後, 當前線程再繼續
join(int), 可以等待指定的毫秒之後繼續

package com.soar.threadmethod;

public class Demo5_Join {
    /*
     * join(), 當前線程暫停, 等待指定的線程執行結束後, 當前線程再繼續
     * join(int), 可以等待指定的毫秒之後繼續
     * 插隊
     */
    public static void main(String[] args) {

        final Thread t1 = new Thread(){
            public void run(){
                for(int i=0; i<10; i++){
                    System.out.println(getName()+"    aaaaaaa");
                }
            }
        };

        Thread t2 = new Thread(){
            public void run(){
                for(int i=0; i<10; i++){
                    if(i==2){
                        try {
                            //t1.join();
                            t1.join(1);     //傳入的參數代表的是插隊的時間,過了指定的時間後,兩條線程交替執行
                        } catch (InterruptedException e) {

                            e.printStackTrace();
                        }
                    }
                    System.out.println(getName()+"    bb");
                }
            }
        };

        t1.start();
        t2.start();
    }

}

Console:

Thread-1    bb
Thread-0    aaaaaaa
Thread-1    bb
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-0    aaaaaaa
Thread-1    bb
Thread-1    bb
Thread-1    bb
Thread-1    bb
Thread-1    bb
Thread-1    bb
Thread-1    bb
Thread-1    bb

四、禮讓線程(yield)(瞭解)

yield讓出cpu

package com.soar.threadmethod;

public class Demo6_Yield {
    /*
     * 禮讓線程
     * yield讓出cpu
     */
    public static void main(String[] args) {
        new MyThread().start();
        new MyThread().start();
    }

}

class MyThread extends Thread{
    public void run(){
        for(int i=1; i<1000; i++){
            if(i%10 == 0){
                Thread.yield();         //讓出CPU
            }
            System.out.println(getName()+"。。。"+i);
        }
    }
}

五、設置優先級線程(setPriority)(瞭解)

setPriority()設置線程的優先級

package com.soar.threadmethod;

public class Demo7_Priority {
    /*
     * setPriority()設置線程的優先級
     */
    public static void main(String[] args) {
        Thread t1 = new Thread(){
            public void run(){
                for(int i=0; i<100; i++){
                    System.out.println(getName()+"    aaaaa");
                }
            }
        };

        Thread t2 = new Thread(){
            public void run(){
                for(int i=0; i<100; i++){
                    System.out.println(getName()+"    aaaaa");
                }
            }
        };

        t1.setPriority(Thread.MIN_PRIORITY);    //設置最小的線程優先級
        t2.setPriority(Thread.MAX_PRIORITY);    //設置最大的線程優先級

        t1.start();
        t2.start();
    }

}
發佈了140 篇原創文章 · 獲贊 15 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章