线程控制守护线程

package Thread.Tread03;

/**
* FileName: TreadControl
* Author: lps
* Date: 2022/3/29 15:37
* Sign:刘品水 Q:1944900433
* void join()
* 等待该线程死亡。
* static void sleep(long millis)
* 当前正在执行的线程休眠(暂停执行)为指定的毫秒数,根据精度和系统定时器和调度的准确性。
* void setDaemon(boolean on)
* 标志着该线程是 daemon线程或用户线程。
*/
public class TreadDaemon extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+","+i);
}
}
}

package Thread.Tread03;

/**
* FileName: ThreadDaemonDemo
* Author: lps
* Date: 2022/3/29 16:01
* Sign:刘品水 Q:1944900433
* * void setDaemon(boolean on)
* * 标志着该线程是 daemon线程或用户线程。
*/
public class ThreadDaemonDemo {
public static void main(String[] args) {
TreadDaemon td1 = new TreadDaemon();
TreadDaemon td2 = new TreadDaemon();
td1.setName("张飞");
td2.setName("关羽");
//设置主线程为刘备
Thread.currentThread().setName("刘备");

//设置守护线程
td1.setDaemon(true);
td2.setDaemon(true);
td1.start();
td2.start();
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
}

}
}

 

posted @ 2022-03-29 16:18  刘品水  阅读(21)  评论(0编辑  收藏  举报