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 ...
随机推荐
- js得到分页栏
自己写的,感觉返回html代码蠢蠢的,但是新手并不知道怎么写更好的,感觉这样子也蛮简单.记录下来,以后来越改越好. //获得分页栏.注意indexSize为奇数,这样也比较好看 //totalNum: ...
- spring的常用配置
bean.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- Linux下载文件时报错 To connect to ohse.de insecurely, use ‘--no-check-certificate’.
远程调用下载文件直接用wget就可以,一般文件路径类型是http.如果有遇到是https就会下载出错. 错误如下: [root@vmu010226-node1 tmp]# wget https://w ...
- js日历,使用datepicker.js,ui.core.js,jquery-1.7.1.js
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 转载纯真ip库
http://blog.csdn.net/clin003/archive/2007/08/14/1743157.aspx 利用 QQWry.Dat 实现 IP 地址高效检索(PHP) 根据 LumaQ ...
- Python学习之路——迭代器、生成器、算法基础、正则
一.迭代器: 迭代器是访问集合元素的一种方式. 迭代器对象是从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. 另外,迭代 ...
- mongoDB初接触
首先去官网http://www.mongodb.org/ 下载mongoDB 下载后解压D:\mongodb 创建两个新文件夹data以及log 新建一个文件命名为mongodb.cfg dbpath ...
- eval以及json
参考 http://www.cnblogs.com/artwl/archive/2011/09/07/2169680.html http://www.cnblogs.com/objectorl/arc ...
- Mylyn
Mylyn(旧称Mylar)是eclipse的一个插件,用于将任务管理和上下文管理无缝集成到Eclipse中.1. 安装 下载相应的Mylyn zip包,解压缩开就是两个文件夹:features和pl ...
- mac 下搭建 Android 开发环境
因工作需要,要在mac 下搭建 Android 开发环境.谷歌.度娘了好久,没有找个一个完整又系统的方法,很是苦恼.最终,皇天不负有心人,找到了下面这篇文档,结合亲身体验,特此记录.也为有这方面需求的 ...