在java线程中有两种线程,一种是用户线程,其余一种是守护线程。

守护线程具有特殊的含义,比如gc线程。当最后一个非守护线程执行完后,守护线程随着jvm一同结束工作。

java中的守护线程需要将Daemon这个属性设置为true时为守护线程

如:

package com.ming.thread.daemonthread;

public class MyThread extends Thread{

    public MyThread(String name){
        super(name);
    }
    
    public void run(){
        for(int i=0;i<10;i++){
            System.out.println("cureentName:"+Thread.currentThread().getName()+"."+"i:"+i);
        }
    }
}

 

package com.ming.thread.daemonthread;

/**
 * 
 * @author ming
 *
 */
public class Run {

    public static void main(String[] args) throws InterruptedException {
        MyThread t=new MyThread("my_daemonthread");
        t.setDaemon(true);//这个属性就是设置自己的的用户线程为守护线程
        t.start();
        Thread.sleep(5000);
        System.out.println("执行完了线程");
    }
    
}

 

更多应用看这里:http://www.cnblogs.com/super-d2/p/3348183.html

 

相关文章:

  • 2022-12-23
  • 2021-11-13
  • 2021-10-14
  • 2022-02-01
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
猜你喜欢
  • 2021-12-26
  • 2021-07-10
  • 2021-07-05
  • 2021-10-10
相关资源
相似解决方案