线程安全-共享资源-守护线程
package com.liujinghe.lianxi1.a_thread;
class MyThread3 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("软件更新中:");
for(int i=0;i<100;i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("更新中:"+i+"%");
}
}
}
public class Demo3 {
public static void main(String[] args) throws InterruptedException {
//创建线程
Thread thread = new Thread(new MyThread3(),"守护");
//将线程设置为守护线程
thread.setDaemon(true);
//启动线程
thread.start();
//编写主线程
for(int i=0;i<50;i++) {
Thread.sleep(100);
System.out.println("主线程运行中");
}
//thread是守护线程,main是被守护的线程
//主线程一旦结束,守护线程也结束
}
}
运行截图