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();
    }

}
posted @ 2017-09-02 17:24  Soar_Sir  阅读(194)  评论(0编辑  收藏  举报