Deamon Thread 讲解
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 讲解的更多相关文章
- Java线程并发:知识点
Java线程并发:知识点 发布:一个对象是使它能够被当前范围之外的代码所引用: 常见形式:将对象的的引用存储到公共静态域:非私有方法中返回引用:发布内部类实例,包含引用. 逃逸:在对象尚未准备 ...
- JAVA并发,线程工厂及自定义线程池
package com.xt.thinks21_2; import java.util.concurrent.ExecutorService; import java.util.concurrent. ...
- Java多线程 - 控制线程
join线程 在某个线程的执行流中调用其他线程的join()方法时,调用线程将被阻塞,直到被join()方法加入的线程完成为止. join()方法有三种重载形式: join():等待被join的线程执 ...
- java多线程(二)
线程的阻塞状态: 参考java多线程(一)多线程的生命周期图解,多线程的五种状态. 1.1 join(),如果在A线程体里面执行了B线程的join()方法,那么A线程阻塞,直到B线程生命周期结 ...
- 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解
序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...
- java基础知识回顾之java Thread类学习(八)--java.util.concurrent.locks(JDK1.5)与synchronized异同讲解
看API文档介绍几个方法: JDK1.5中提供了多线程的升级解决方案: 特点: 1.将同步synchronized显示的替换成Lock 2.接口Conditio ...
- JVM内的守护线程Deamon与用户线程User Thread
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6561771.html 一:守护线程Daemon 守护线程:Daemon在希腊神话中解作“守护神”,顾名思义就 ...
- 性能分析之-- JAVA Thread Dump 分析综述
性能分析之-- JAVA Thread Dump 分析综述 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工 ...
- thread dump
最近在做性能测试,需要对线程堆栈进行分析,在网上收集了一些资料,学习完后,将相关知识整理在一起,输出文章如下. 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Du ...
随机推荐
- java执行windows 的cmd 命令
//获取运行时 Runtime rt = Runtime.getRuntime(); //获取进程 Process p = rt.exec(String[] cmdarray); 或者 P ...
- Python网络编程——设备名和IPv4地址
1.快速查看主机名和对应的IP地址小程序 import socket def print_machine_info(): # 定义print_machine_info()类 host_name = s ...
- WebHdfs
https://github.com/ihrwein/webhdfs https://tiborbenke.blogs.balabit.com/2013/11/the-syslog-ng-in-the ...
- 第二节 EAN 8 码 / EAN 13 码
EAN码的全名为欧洲商品条码(European Article Number),源於西元1977年,由欧洲十二个工业国家所共同发展出来的一种条码.目前已成为一种国际性的条码系统.EAN条码系统的管理是 ...
- VC6中创建Qt工程的创建
文章来源:http://blog.sina.com.cn/s/blog_64d015c10100sf1o.html 本文主要介绍怎么创建可以在VC6中编译的QT工程.本文所采用环境为VC++6.0+Q ...
- 关于 firefox 无法在 passport.csdn.net 找到该服务器
很奇怪的现象:用firefox上网,某些网站打开总是会提示 无法在XXX找到该服务器.但是使用其他浏览器,比如360却可以正常打开. 我已经将firefox加入了防火墙的信任列表,但是仍旧是这样. 而 ...
- Swift - 2 (?和!、结构体、类、协议、扩展、闭包)
1> 可选类型(?)和强制解包(!) 在swift中,可选类型(?) 其根源是一个 枚举型,里面有 None 和 Some 两种类型.其实所谓的 nil 就是 Optional.None , 非 ...
- android Graphics(一):概述及基本几何图形绘制
前言:我最近想抽空研究研究android的各种特效,android的特效真是其它平台无法比拟的,而且一个漂亮的UI交互,会给APP增色不少,而学习特效之前,有关graphics绘图的基础知识是必不可少 ...
- Java线程(十):CAS
前言 在Java并发包中有这样一个包,java.util.concurrent.atomic,该包是对Java部分数据类型的原子封装,在原有数据类型的基础上,提供了原子性的操作方法,保证了线程安全.以 ...
- ThinkPHP - 自动创建 + 自动验证 + 自动完成
自动创建:创建数据模型. $User->create(); 自动验证:验证提交的表单数据. protected $_validate = array( array('verify','requi ...