The daemon thread's life cycle is same with the life cycle of the application which starts this daemon thread. If the application finishes, daemon threads are terminated at the same time. An example for a daemon thread is the garbage collection.

"Daemon Thread" is also called "Service Thread"; normal threads are called "User Thread".

Additionally, please do not use daemon threads to do I/O operations.

The following example is that one application starts a user thread which runs a daemon thread with infinity loop.

The user thread dies first, but the daemon thread continues.

If the application dies, the daemon thread is dead immediately.

public class DaemonThread extends Thread {
    public DaemonThread() {
        this.setDaemon(true);
    }
    @Override
    public void run() {
        while (true) {
            System.out.println("Begin to sleep.");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Wake up.");
        }
    }
}

public class MainApp implements Runnable{
    
    @Override
    public void run(){
        DaemonThread dt = new DaemonThread();
        dt.start();
        System.out.println("The user thread is terminated.");
    }

public static void main(String[] args) {
        Thread thread = new Thread(new MainApp());
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("The main thread is terminated.");
    }

}

Output:

The user thread is terminated.
Begin to sleep.
Wake up.
Begin to sleep.
Wake up.
Begin to sleep.
The main thread is terminated.

Deamon Thread 讲解的更多相关文章

  1. Java线程并发:知识点

    Java线程并发:知识点   发布:一个对象是使它能够被当前范围之外的代码所引用: 常见形式:将对象的的引用存储到公共静态域:非私有方法中返回引用:发布内部类实例,包含引用.   逃逸:在对象尚未准备 ...

  2. JAVA并发,线程工厂及自定义线程池

    package com.xt.thinks21_2; import java.util.concurrent.ExecutorService; import java.util.concurrent. ...

  3. Java多线程 - 控制线程

    join线程 在某个线程的执行流中调用其他线程的join()方法时,调用线程将被阻塞,直到被join()方法加入的线程完成为止. join()方法有三种重载形式: join():等待被join的线程执 ...

  4. java多线程(二)

    线程的阻塞状态: 参考java多线程(一)多线程的生命周期图解,多线程的五种状态.     1.1 join(),如果在A线程体里面执行了B线程的join()方法,那么A线程阻塞,直到B线程生命周期结 ...

  5. 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解

    序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...

  6. java基础知识回顾之java Thread类学习(八)--java.util.concurrent.locks(JDK1.5)与synchronized异同讲解

    看API文档介绍几个方法:  JDK1.5中提供了多线程的升级解决方案: 特点: 1.将同步synchronized显示的替换成Lock                    2.接口Conditio ...

  7. JVM内的守护线程Deamon与用户线程User Thread

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6561771.html  一:守护线程Daemon 守护线程:Daemon在希腊神话中解作“守护神”,顾名思义就 ...

  8. 性能分析之-- JAVA Thread Dump 分析综述

    性能分析之-- JAVA Thread Dump 分析综述       一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工 ...

  9. thread dump

    最近在做性能测试,需要对线程堆栈进行分析,在网上收集了一些资料,学习完后,将相关知识整理在一起,输出文章如下. 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Du ...

随机推荐

  1. C++的发展,特点和源程序构成

    最近一段时间在学习C++,也借了几本相关的书籍.因为之前主要用C#写程序,大概写了也有两年了吧.所以在回过头来学习C++,还是挺快的.但是我觉得光看书是不行的,要写!!因此我想把我整个学习C++的过程 ...

  2. JavaSE学习总结第19天_IO流1

      19.01  集合的特点和数据结构总结 HashSet.HashMap.Hashtable判断元素唯一性的方式: 通过对象的hashCode和equals方法来完成元素唯一性 如果对象的hashC ...

  3. linux下出现java.net.UnknownHostException

    项目部署在win环境下没问题,但是在迁移生产环境的时候出现Java.net.UnknowHostException 原因在于etc/hosts 文件没有配置域名映射,使用vi编辑器加上服务器ip 以及 ...

  4. Servlet乘法表学习笔记

    一.控制台实现乘法表 package com.shanrengo; import java.io.IOException; import java.io.PrintWriter; import jav ...

  5. ThinkPHP第六天(正则表达式)

    1.正则表达式:原子 ①a-z,A-Z,0-9,_ ②用(abc)圆括号括起来的单元符号,表示括号里面的作为一个完整的组合,必须完成匹配,不被拆分来 ③用方括号括起来[abc][^abc]称之为原子表 ...

  6. (Problem 29)Distinct powers

    Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...

  7. mysql在linux上的一点操作

    1,查看打开端口. show variables like 'port'; 2, 指定ip,用户名,密码 1 grant all privileges on   *.* to root@"% ...

  8. QT基本数据类型(以前没见过qintptr和qlonglong)

    QT的基本数据类型 qint8:signed char 有符号8比特数据 qint16:signed short 16位数据类型 qint32:signed int. 32位有符号数据类型 qint6 ...

  9. Uva 225 Golygons

    这道题如果直接用Dfs,运气好的话是可以直接过的. 但如果要在Dfs的基础上加快速度,剪枝是必不可少的. 我的剪枝策略: 1.当前点(x,y)回到出发点至少需要 |x| +| y| 步,如果剩余的步数 ...

  10. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553   ...