Java多线程之后台线程

时间:2014-05-25 13:44:02   收藏:0   阅读:276

将线程设置成后台线程Daemons 主线程结果后,后台线程将自动结果。

 

bubuko.com,布布扣
package wzh.test;

import java.util.concurrent.TimeUnit;

 class SimpleDaemons implements Runnable{

    @Override
    public void run() {
        try {
            while (true) {
                TimeUnit.MILLISECONDS.sleep(100);
                System.out.println(Thread.currentThread().getId()+"  "+this);
            }
        } catch (Exception e) {
            System.out.println("sleep() interrupted");
        }
    }
}

public class SimpleDaemonsMain{
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Thread daemon=new Thread(new SimpleDaemons());
            daemon.setDaemon(true);
            daemon.start();
        }
        System.out.println(Thread.currentThread().getId()+"All daemons started");
        TimeUnit.MICROSECONDS.sleep(175234231);
    }
    
}
bubuko.com,布布扣

 

Java多线程之后台线程,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!