Java 多线程学习笔记(三)-守护线程

来源:互联网 发布:马赛克视频还原软件 编辑:程序博客网 时间:2024/06/05 05:36
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 blocke.printStackTrace();}}}
package testpackage;public class MyThread extends Thread {private int i = 0;@Overridepublic void run() {try {while (true) {i++;System.out.println("i=" + (i));Thread.sleep(1000);}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
i=1
i=2
i=3
i=4
i=5

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





2 0
原创粉丝点击