在 JAVA中的CountDownLatch、CyclicBarrier、Semaphore的简单测试 这文章里说到了线程的daemon问题,特写一篇来分析一下。

上代码:

 package com.yzl.dubbo;

 import java.util.concurrent.TimeUnit;

 /**
* java Thread的daemon属性测试
* 结论:
* 1、当虚拟机不存在daemon==false的线程时,虚拟机将会自动退出
* 2、当虚拟机退出时,daemon里的finally代码不一定会执行完全(可能执行到一半就被强制干掉了)
* @author yangzhilong
*
*/
public class Daemon { public static void main(String[] args) {
Thread thread = new Thread(new DaemonRunner(), "daemon");
//当虚拟机不存在daemon==false的线程时,虚拟机将会自动退出
//mian线程属于false
//不对这个属性进行设置的线程也是false
// thread.setDaemon(false);
thread.setDaemon(true);
thread.start();
} static class DaemonRunner implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("run ........");
TimeUnit.SECONDS.sleep(5);
System.out.println("end run......");
} catch (Exception e) {
} finally {
System.out.println("finally is runing。。。。");
}
}
}
}
}

注释掉20行,放开21行的运行结果如下:

run ........

注释掉21行,放开20行的运行结果如下:

run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。
run ........
end run......
finally is runing。。。。

我们来看看Thread的构造函数里的核心源码:

 private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc) {
if (name == null) {
throw new NullPointerException("name cannot be null");
} this.name = name; Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */ /* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
g = security.getThreadGroup();
} /* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
} /* checkAccess regardless of whether or not threadgroup is
explicitly passed in. */
g.checkAccess(); /*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
} g.addUnstarted(); this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext =
acc != null ? acc : AccessController.getContext();
this.target = target;
setPriority(priority);
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize; /* Set thread ID */
tid = nextThreadID();
}

被new出来的thread如果没有特别设置它的daemon属性,那它的daemon将和创建它的线程的daemon相同,Junit线程是daemon=true(后台线程),所以new出来的线程也是后台线程,当虚拟机中不存在daemon==false的线程后,虚拟机将会自动退出。

关于java线程的daemon的认识的更多相关文章

  1. Java并发编程(三)后台线程(Daemon Thread)

    后台线程,守护线程(Daemon Thread) 所谓的后台线程,就是指这种线程并不属于程序中不可或缺的部分,因此当所有的非后台线程结束时,程序也就终止了,同时会杀死进程中的所有后台线程.通过setD ...

  2. Java 多线程(四)之守护线程(Daemon)

    目录 定义 如何创建 判断 注意事项 函数setDaemon(true)必须在 start() 函数之前使用. 守护线程中产生的线程也是守护线程: 测试 @ 定义 Java 中有两种线程: 一种是用户 ...

  3. java线程基础知识----java daemon线程

    java线程是一个运用很广泛的重点知识,我们很有必要了解java的daemon线程. 1.首先我们必须清楚的认识到java的线程分为两类: 用户线程和daemon线程 A. 用户线程: 用户线程可以简 ...

  4. Java 守护线程(Daemon) 例子

    当我们在Java中创建一个线程,缺省状态下它是一个User线程,如果该线程运行,JVM不会终结该程序.当一个线被标记为守护线程,JVM不会等待其结束,只要所有用户(User)线程都结束,JVM将终结程 ...

  5. java线程 - 多线程 - 守护线程

    1.多线程执行者/处理类 都是Runnable的实现类(如自定义类实现Runnable 或 java原生的Thread.FutureTask),但最后都必须封装成Thread线程类由Thread.st ...

  6. 怎样分析java线程堆栈日志

    注: 该文章的原文是由 Tae Jin Gu 编写,原文地址为 How to Analyze Java Thread Dumps 当有障碍,或者是一个基于 JAVA 的 WEB 应用运行的比预期慢的时 ...

  7. JAVA线程基础

    一.线程状态 由于参考的维度不一样,线程状态划分也不一样,我这里简单的分为5大类,并且会说明状态变迁的详细过程:

  8. Java线程与多线程教程

    本文由 ImportNew - liken 翻译自 Journaldev.   Java线程是执行某些任务的轻量级进程.Java通过Thread类提供多线程支持,应用可以创建并发执行的多个线程. 应用 ...

  9. Java线程池学习

    Java线程池学习 Executor框架简介 在Java 5之后,并发编程引入了一堆新的启动.调度和管理线程的API.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java ...

随机推荐

  1. 深入理解多线程(五)—— Java虚拟机的锁优化技术

    本文是<深入理解多线程>的第五篇文章,前面几篇文章中我们从synchronized的实现原理开始,一直介绍到了Monitor的实现原理. 前情提要 通过前面几篇文章,我们已经知道: 1.同 ...

  2. nfd指令的详细说明

    在eterm上执行NFD:SHAPEK/CA*OW指令,返回如下: LN CXR OW RT FBC/TC RBD MIN/MAX TRVDATE R 01 CA 450.00 U U 00D/00D ...

  3. 在linux机器上面安装anaconda和相关软件

    直接安装anaconda参考这里,主要两条命令: wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh bash ...

  4. 跨平台APP----对Cordova,APPCan,DCloud,APICloud四大平台的分析

    前言: 移动开发是未来一个很重要的IT领域,而跨平台开发将是这一领域最重要的事情.         ----谷震平 一 兵器谱 在国外,最大的是Cordova(PhoneGap,2011年广泛流行), ...

  5. Linq-单条数据删除

    单条数据删除,用DeleteOnSubmit NorthwindDataContext db = new NorthwindDataContext(); Customer test1 = ctx.Cu ...

  6. 【转】BFC是什么

    原文:https://www.cnblogs.com/mlw1814011067/p/10397999.html ------------------------------------------- ...

  7. windows 通过Web.config添加mimetype映射

    在Web.config里添加以下代码即可 <configuration> <system.webServer> <staticContent> <!-- re ...

  8. 【math】梯度下降法(梯度下降法,牛顿法,高斯牛顿法,Levenberg-Marquardt算法)

    原文:http://blog.csdn.net/dsbatigol/article/details/12448627 何为梯度? 一般解释: f(x)在x0的梯度:就是f(x)变化最快的方向 举个例子 ...

  9. 一次jdbc乱码解决

    今天我做了一个小实验,从sqlserver 2010中将一张表转移到mysql中,使用的是基本的jdbc,前面复制的好好地,不知道怎么了,到了第三万行,突然出现了下面的异常 Incorrect str ...

  10. Ubuntu 突然上不去网了怎么办

    到家了也想看看程序.打开WIN8上的虚拟机VM,然后启动Ubuntu.................................... 像往常一样等待着界面,输入password,然后改动程序. ...