您的位置:首页 > 其它

守护线程

2015-10-19 15:43 288 查看
package comm;

class StopThread implements Runnable {

public synchronized void run() {
while (true) {
try {
wait();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()
+ "....exception");
}
System.out.println(Thread.currentThread().getName() + "....run");
}
}
}

public class StopThreadDemo {

public static void main(String[] args) {

StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.setDaemon(true);  //守护线程要在start之前设置,
t2.setDaemon(true);
t1.start();
t2.start();

int num = 0;
while (true) {
if (num++ == 10) {
break;
}
System.out.println(Thread.currentThread().getName() + "------"
+ num);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: