5.2 后台线程与联合线程

来源:互联网 发布:阿里云邮箱账号格式 编辑:程序博客网 时间:2024/06/09 22:01

p { margin-bottom: 0.21cm; }

后台线程如果我们对某个线程在启动(调用start方法)之前 调用setDaemon(true)方法,这个线程就变成了后台线程

 

java程序来说只要还有一个前台线程在运行 这个进程就不会结束,如果一个进程中只有后台线程运行,这个线程就会结束

publicclassTheadDemo1 {

publicstaticvoidmain(String[] args) {

TestTheadtestThead = newTestThead();

testThead.setDaemon(true);

testThead.start();

System.err.println("sdf");

//while(true){

//System.out.println("main"+Thread.currentThread().getName());

//}

}

}

 

classTestThead extendsThread {

@Override

publicvoidrun() {

//TODOAuto-generated method stub

while(true){

System.out.println("run()"+Thread.currentThread().getName());

super.run();

}

}

原创粉丝点击