守护线程是什么_守护线程什么时候结束

守护线程是什么_守护线程什么时候结束守护线程是什么?Java线程分为用户线程和守护线程。守护线程是程序运行的时候在后台提供一种通用服务的线程。所有用户线程停止,进程会停掉所有守护线程,退出程序。Java中把线程设置为守护线程的方法:在start线程之前调用线程的setDaemon(true)方法。注意:setDaemon(true)必须在start()之前设置,否则会抛出IllegalThrea…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

守护线程是什么?

Java线程分为用户线程和守护线程。

守护线程是程序运行的时候在后台提供一种通用服务的线程。所有用户线程停止,进程会停掉所有守护线程,退出程序。

Java中把线程设置为守护线程的方法:在 start 线程之前调用线程的 setDaemon(true) 方法。

注意:

  • setDaemon(true) 必须在 start() 之前设置,否则会抛出IllegalThreadStateException异常,该线程仍默认为用户线程,继续执行
  • 守护线程创建的线程也是守护线程
  • 守护线程不应该访问、写入持久化资源,如文件、数据库,因为它会在任何时间被停止,导致资源未释放、数据写入中断等问题
public class TestDaemonThread {

	public static void main(String[] args) {
		testDaemonThread();
	}
	
	public static void testDaemonThread() {
		Thread t = new Thread() {

			@Override
			public void run() {
				//创建线程,校验守护线程内创建线程是否为守护线程
				Thread t2 = new Thread() {

					@Override
					public void run() {
						System.out.println("t2 : " + (Thread.currentThread().isDaemon() ? "daemon thread" : "non-daemon thread"));
					} 
					
				};
				t2.start();
				
				//当所有用户线程执行完,守护线程会被直接杀掉,程序停止运行
				int i = 1;
				while(true) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("t : " + (Thread.currentThread().isDaemon() ? "daemon thread" : "non-daemon thread") + " , time : " + i);
					if (i++ >= 10) {
						break;
					}
				}
			}
			
		};
//		t.start(); //setDaemon(true) 必须在 start() 之前设置,否则会抛出IllegalThreadStateException异常,该线程仍默认为用户线程,继续执行
		t.setDaemon(true);
		t.start();
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

执行结果:

t2 : daemon thread
t : daemon thread , time : 1
t : daemon thread , time : 2

结论:

  • 上述代码线程t,未打印到 t : daemon thread , time : 10,说明所有用户线程停止,进程会停掉所有守护线程,退出程序
  • 当 t.start(); 放到 t.setDaemon(true); 程序抛出IllegalThreadStateException,t 仍然是用户线程,打印如下
Exception in thread "main" t2 : non-daemon thread
java.lang.IllegalThreadStateException
	at java.lang.Thread.setDaemon(Thread.java:1359)
	at constxiong.interview.TestDaemonThread.testDaemonThread(TestDaemonThread.java:42)
	at constxiong.interview.TestDaemonThread.main(TestDaemonThread.java:6)
t : non-daemon thread , time : 1
t : non-daemon thread , time : 2
t : non-daemon thread , time : 3
t : non-daemon thread , time : 4
t : non-daemon thread , time : 5
t : non-daemon thread , time : 6
t : non-daemon thread , time : 7
t : non-daemon thread , time : 8
t : non-daemon thread , time : 9
t : non-daemon thread , time : 10

 


【Java面试题与答案】整理推荐

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181562.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ffmpeg添加视频封面_ffmpeg提取波形文件

    ffmpeg添加视频封面_ffmpeg提取波形文件ffmpeg-ia.mp4-y-fimage2-frames1a.jpgffmpeg-i11.mp4-vframes1xx.jpgffmpeg-ia.mp4-r0.1frames_%04.pngconvert-backgroundwhite-flatten***.pdf***.png

    2025年6月14日
    0
  • Calling Matlab function from python: “initializer must be a rectangular nested sequence”

    Calling Matlab function from python: “initializer must be a rectangular nested sequence”

    2021年11月21日
    45
  • navicat 15 fro Mysql激活码(注册激活)

    (navicat 15 fro Mysql激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlHQ…

    2022年3月28日
    82
  • RBAC权限管理

    RBAC权限管理

    2021年11月29日
    38
  • 大数据数仓建模

    大数据数仓建模        大数据项目之电商数仓(用户行为数据采集)数据仓库简介      1.什么是数据库?    数据库(database)是按照数据结构来组织,存储和管理数据的建立在计算机存储设备上的仓库。    数据库是长期存储在计算机内,有组织的,可共享的数据集合。数据库中的数据指的是以一定的数据模型组织,描述和存储在一起,具有尽可能小的冗余度,较高的数据独立性和易扩展性的特点并可在一定范围内为多个用户共享。    常用的数据库有mysql,oracle,sqlserver等。作用不一样,数据库是

    2022年5月7日
    47
  • linux下开启、关闭、重启mysql服务命令

    linux下开启、关闭、重启mysql服务命令一、启动1、使用service启动:servicemysqlstart2、使用mysqld脚本启动:/etc/inint.d/mysqlstart3、使用safe_mysqld启动:safe_mysql&二、停止1、使用service启动:servicemysqlstop2、使用mysqld脚本启动:/etc/inint.d/mysqlstop3、mysqladminshutdown三、重启1、使用service启动:service.

    2022年10月29日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号