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++动态库 封送结构体,结构体数组

    因为实验室图像处理的算法都是在OpenCV下写的,还有就是导航的算法也是用C++写的,然后界面部分要求在C#下写,所以不管是Socket通信,还是调用OpenCV的DLL模块,都设计到了C#和C++数 ...

  2. UNIX网络编程5 POSIX 消息队列

    <mqueue.h> mq_open mq_close mq_unlink mq_getattr/mq_setattr mq_send/mq_receive mq_notify sigwa ...

  3. [lua]笔试-组合概率

    --[[ 组合概率 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  4. QMessageBox 用法

    案例一:QMessageBox msgBox;msgBox.setText("The document has been modified.");msgBox.setInforma ...

  5. AnyEvent::HTTP 介绍

    AnyEvent::HTTP - simple but non-blocking HTTP/HTTPS client 一个简单的非堵塞的 HTTP/HTTPS 客户端: use AnyEvent::H ...

  6. 基于visual Studio2013解决C语言竞赛题之0305显示星期

     题目 解决代码及点评 这道题锻炼我们switch分支语句,对于条件太多时,用if符合条件分支是比较复杂的 可以使用switch代替 //5. 读入1到7之间的某个数,输出表示一星期中相应的 // ...

  7. css中的定位

    上一篇博客,我大概介绍了下浮动的使用及行为.其实在整个文档布局中,定位也对我们整个的页面排版有非常好的帮助,当然前提是使用得当. 一.定位分类: a.静态定位  position:static;   ...

  8. ThinkPHP - 登录流程

    数据库: /* Navicat MySQL Data Transfer Source Server : 本地连接 Source Server Version : 50710 Source Host : ...

  9. 写一个方法完成如下功能,判断从文本框textbox1输入的一个字符,如果是数字则求该数字的阶乘,如果是小写字条,则转换为大写,大写字符不变,结果在文本框textbox2中显示

    窗体设计: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  10. hdu 4784 Dinner Coming Soon

    spfa+优先队列.刚开始只用的spfa,结果tle到死.然后听队友说要用到优先队列,想了想,对时间分层的话的确每一个结点都只进队列一次即可,因为只有大时间才能更新出小时间,然后就wa成shi了.按队 ...