What is Daemon Thread in Java? Example Attached
来源:互联网 发布:数据结构与算法分析c 编辑:程序博客网 时间:2024/05/16 09:47
(http://crunchify.com/what-is-daemon-thread-in-java-example-attached/)
Daemon threads in Java are like a service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.
When a new thread is created it inherits the daemon status of its parent. Normal thread and daemon threads differ in what happens when they exit. When the JVM halts any remaining daemon threads are abandoned: finally blocks are not executed, stacks are not unwound – JVM just exits. Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.
setDaemon(true/false) ?
This method is used to specify that a thread is daemon thread.
public boolean isDaemon() ?
This method is used to determine the thread is daemon thread or not.
Java Example:
package com.crunchify.tutorials; /** * @author Crunchify.com */ public class CrunchifyDaemonThread extends Thread {public static void main(String[] args) {System.out.println("Main Method Entry"); CrunchifyDaemonThread t = new CrunchifyDaemonThread();t.setDaemon(true);// When false, (i.e. when it's a user thread), the Worker thread// continues to run.// When true, (i.e. when it's a daemon thread), the Worker thread// terminates when the main thread terminates.t.start(); try {Thread.sleep(3000);} catch (InterruptedException x) {} System.out.println("Main Method Exit");} public void run() {System.out.println("run Method Entry"); try {System.out.println("In run Method: currentThread() is"+ Thread.currentThread()); while (true) {try {Thread.sleep(1000);} catch (InterruptedException x) {} System.out.println("In run method.." + Thread.currentThread());}} finally {System.out.println("run Method Exit");}}}
run Method Entry
In run Method: currentThread() isThread[Thread-0,5,main]
In run method..Thread[Thread-0,5,main]
In run method..Thread[Thread-0,5,main]
Main Method Exit
ends the program
finishing the thread.Output2: (with t.setDaemon(false)):
with t.setDaemon(false);run Method Entry
In run Method: currentThread() isThread[Thread-0,5,main]
In run method..Thread[Thread-0,5,main]
In run method..Thread[Thread-0,5,main]
Main Method Exit
In run method..Thread[Thread-0,5,main]
In run method..Thread[Thread-0,5,main]
In run method..Thread[Thread-0,5,main]
In run method..Thread[Thread-0,5,main]
...
...
...
program doesn't exit
when it’s not a daemon thread.- What is Daemon Thread in Java? Example Attached
- What is CountDownLatch in Java
- What is CyclicBarrier in Java
- what is thread safe
- Java - What is final in Java?
- Java - What is Abstraction in Java
- java Daemon Thread User Thread
- JAVA的daemon Thread
- Java Daemon Thread
- What exactly is null in Java?[转]
- What is autoboxing and unboxing in Java
- What is Instance Initializer in Java?
- What is Java thread priority? 什么是java线程优先级
- A easy example to tell you what is "2>&1" in Perl
- JAVA Thread Example
- JAVA守护线程 daemon thread
- User Thread与Daemon Thread- java
- what is Class Object(java.lang.class) in java?
- 解决virtualBox ubuntu桥接,nat不能上网问题
- 跨应用启动/绑定Service
- iOS Lable根据文本数量动态设置frame
- eclipse 中修改tomcat内存
- 对象/函数对象的原型
- What is Daemon Thread in Java? Example Attached
- POJ2139 Six Degrees of Cowvin Bacon
- 2326: [HNOI2011]数学作业
- 表单实现登录注册功能
- 前端面试宝典超
- 汇编语言的"hello world"
- Postman输入List参数
- Java实现网页截图技术,根据URL得到网页快照
- C语言再学习 -- 循环语句