您的位置:首页 > 编程语言 > Java开发

[置顶] Java 多线程学习笔记(三)-守护线程

2016-07-03 15:36 531 查看
package test.run;

import testpackage.MyThread;

public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.setDaemon(true);
thread.start();
Thread.sleep(5000);
System.out.println("我离开thread对象也不再打印了,也就是停止了!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package testpackage;

public class MyThread extends Thread {
private int i = 0;

@Override
public void run() {
try {
while (true) {
i++;
System.out.println("i=" + (i));
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
i=1

i=2

i=3

i=4

i=5
我离开thread对象也不再打印了,也就是停止了!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: