线程控制

1.join

package com.thread.control;

public class ControlByJoin extends Thread{
    public ControlByJoin(String name){
        super(name);
    }    
    
    public void run(){
        for(int i=0;i<100;i++){
            System.out.println("current Thread name:" + Thread.currentThread().getName() + " "+
                    "Num:" + i);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int i=0;i<100;i++){
            
            
            if(i==20){
                ControlByJoin cj = new ControlByJoin("the thread is joined");
                cj.start();
                try {
                    cj.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                
            }
            System.out.println("current Thread name:" + Thread.currentThread().getName() + " "+ 
                    "Num:" + i);
        }
    }

}

运行结果

...
current Thread name:main Num:17
current Thread name:main Num:18
current Thread name:main Num:19
current Thread name:the thread is joined Num:0
current Thread name:the thread is joined Num:1
current Thread name:the thread is joined Num:2
current Thread name:the thread is joined Num:3
...
current Thread name:the thread is joined Num:96
current Thread name:the thread is joined Num:97
current Thread name:the thread is joined Num:98
current Thread name:the thread is joined Num:99
current Thread name:main Num:20
current Thread name:main Num:21
current Thread name:main Num:22
...
current Thread name:main Num:95
current Thread name:main Num:96
current Thread name:main Num:97
current Thread name:main Num:98
current Thread name:main Num:99

 2. SetDaemon

package com.thread.control;

public class ControlBySetDaemon extends Thread{
    public ControlBySetDaemon(String name){
        super(name);
    }
    public void run(){
        for (int i=0;i<1000;i++){
            System.out.println(getName()+" "+i);
        }
    }

    public static void main(String[] args) {
        ControlBySetDaemon sd=new ControlBySetDaemon("Daemon Thread");
        sd.setDaemon(true);
        sd.start();
        for (int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName()+" "+i);
        }
    }
}

//从运行结果可以看到,Daemon Thread并没有运行到自然死亡,而是在前台mian线程结束后被死亡了。

3.sleep

package com.thread.control;

public class ControlBySleep extends Thread{
    public ControlBySleep(String name){
        super(name);
    }
    
    public void run(){
        for(int i=0 ; i<100 ; i++){
            System.out.println(getName() + " " + i);
        }
    }

    public static void main(String[] args) {
        ControlBySleep s = new ControlBySleep("new Thread");
        s.start();
        for(int i=0 ; i<100 ; i++){
            System.out.println(Thread.currentThread().getName() + " " + i);
            //当i为20时,主线程sleep3秒
            if (i==20){
                System.out.println("begin sleep");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }            
                System.out.println("end sleep");
            }
        }        
    }

}

运行结果

从结果中可以看到在主线程sleep的3秒中,new Thread已经完成运行。

...
main 17
new Thread 17
main 18
new Thread 18
main 19
new Thread 19
main 20
new Thread 20
begin sleep
new Thread 21
new Thread 22
new Thread 23
new Thread 24
....
new Thread 95
new Thread 96
new Thread 97
new Thread 98
new Thread 99
end sleep
main 21
main 22
main 23
main 24
main 25
....

 4.yield

package com.thread.control;

public class ControlByYield extends Thread{
    public ControlByYield(String name){
        super(name);
    }
    
    public void run(){
        for(int i=0;i<100;i++){
            System.out.println(getName()+ " " + i);
            if(i==20){
                Thread.yield();                
            }
        }
    }

    public static void main(String[] args) {
        ControlByYield y1=new ControlByYield("Thread 1");
        y1.setPriority(MAX_PRIORITY);
        ControlByYield y2=new ControlByYield("Thread 2");
        y2.setPriority(MIN_PRIORITY);
        y1.start();
        y2.start();
    }

}

运行结果

从运行结果上可以看到优先执行了Thread 1

...
Thread 1 95
Thread 1 96
Thread 1 97
Thread 1 98
Thread 1 99
Thread 2 1
Thread 2 2
Thread 2 3
Thread 2 4
Thread 2 5
...

 5. setPriority

package com.thread.control;

public class ControlByPriority extends Thread{
    public ControlByPriority(String name){
        super(name);
    }
    
    public void run(){
        for(int i = 0; i<100;i++){
            System.out.println(getName() +" " +"The Priority is : " + 
        getPriority() + " "+"The value of i is : "+ i);
        }
    }

    public static void main(String[] args) {
        Thread.currentThread().setPriority(6);
        System.out.println(Thread.currentThread().getName()+ " "+"The Priority of main thread is : "+ 
                Thread.currentThread().getPriority());
        for(int i=0;i<100;i++){
            if(i==10){
                ControlByPriority low =new ControlByPriority("low");
                low.start();
                System.out.println("The Priority of thread low is : "+ 
                        low.getPriority());
                low.setPriority(MIN_PRIORITY);
            }
            
            if(i==20){
                ControlByPriority high =new ControlByPriority("high");
                high.start();
                System.out.println("The Priority of thread high is : "+ 
                        high.getPriority());
                high.setPriority(MAX_PRIORITY);
            }            
        }
    }
}

 

posted @ 2016-05-15 17:28  月色深潭  阅读(225)  评论(0编辑  收藏  举报