用户线程和守护线程

用户线程

用户线程执行完,jvm退出。守护线程还是可以跑的

/**
* A <i>thread</i> is a thread of execution in a program. The Java
* Virtual Machine allows an application to have multiple threads of
* execution running concurrently.
* <p>
* Every thread has a priority. Threads with higher priority are
* executed in preference to threads with lower priority. Each thread
* may or may not also be marked as a daemon. When code running in
* some thread creates a new <code>Thread</code> object, the new
* thread has its priority initially set equal to the priority of the
* creating thread, and is a daemon thread if and only if the
* creating thread is a daemon.
* <p>
* When a Java Virtual Machine starts up, there is usually a single
* non-daemon thread (which typically calls the method named
* <code>main</code> of some designated class). The Java Virtual
* Machine continues to execute threads until either of the following
* occurs:
* <ul>
* <li>The <code>exit</code> method of class <code>Runtime</code> has been
* called and the security manager has permitted the exit operation
* to take place.
* <li>All threads that are not daemon threads have died, either by
* returning from the call to the <code>run</code> method or by
* throwing an exception that propagates beyond the <code>run</code>
* method.
* </ul>
* /

用户线程优先权

例子

package com.java.javabase.thread.base;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PriorityTest {
private static int size =10;
public static void main(String[] args) {
Thread t1 =new ThreadOne("t1");
Thread t2 =new ThreadOne("t2");
t2.setPriority(1);
t1.start();
t2.start();
log.info("Thread {} prority is {}",Thread.currentThread().getName(),Thread.currentThread().getPriority()); }
static class ThreadOne extends Thread{
public ThreadOne(String name){
super(name);
} @Override
public void run(){
int i =0;
while(i<size){
log.info("Thread : {} priority is {} ,run {} times",Thread.currentThread().getName(),
Thread.currentThread().getPriority(),i++);
}
}
}
}

说明

setPriority是Thread方法,用户线程的优先级是1到10,默认是5。虽然设置了优先级,但线程的执行还是在于获取cpu的执行,看操作系统的支持,

不是你级别高,cpu就给你用的。

守护线程

package com.java.javabase.thread.base;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DaemonTest {
private static int size =10;
public static void main(String[] args) {
Thread t1 =new ThreadOne("t1");
Thread t2 =new ThreadTwo("t2");
t2.setDaemon(true);
t1.start();
t2.start();
log.info("Thread {} prority is {}",Thread.currentThread().getName(),Thread.currentThread().getPriority()); }
static class ThreadOne extends Thread{
public ThreadOne(String name){
super(name);
} @Override
public void run(){
int i =0;
while(i<size){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Thread : {} priority is {} ,run {} times",Thread.currentThread().getName(),
Thread.currentThread().getPriority(),i++);
}
}
}
static class ThreadTwo extends Thread{
public ThreadTwo(String name){
super(name);
} @Override
public void run(){
int i =0;
while(true&& i<(size*10000)){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Thread : {} priority is {} ,run {} times",Thread.currentThread().getName(),
Thread.currentThread().getPriority(),i++);
}
}
}
}

测试结果

2019-07-30 20:49:51,963   [main] INFO  DaemonTest  - Thread main prority is 5
2019-07-30 20:49:51,970 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 0 times
2019-07-30 20:49:51,980 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 1 times
2019-07-30 20:49:51,980 [t2] INFO DaemonTest - Thread : t2 priority is 5 ,run 0 times
2019-07-30 20:49:51,991 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 2 times
2019-07-30 20:49:52,001 [t2] INFO DaemonTest - Thread : t2 priority is 5 ,run 1 times
2019-07-30 20:49:52,002 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 3 times
2019-07-30 20:49:52,013 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 4 times
2019-07-30 20:49:52,021 [t2] INFO DaemonTest - Thread : t2 priority is 5 ,run 2 times
2019-07-30 20:49:52,023 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 5 times
2019-07-30 20:49:52,034 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 6 times
2019-07-30 20:49:52,042 [t2] INFO DaemonTest - Thread : t2 priority is 5 ,run 3 times
2019-07-30 20:49:52,045 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 7 times
2019-07-30 20:49:52,056 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 8 times
2019-07-30 20:49:52,062 [t2] INFO DaemonTest - Thread : t2 priority is 5 ,run 4 times
2019-07-30 20:49:52,066 [t1] INFO DaemonTest - Thread : t1 priority is 5 ,run 9 times

java并发:初探用户线程和守护线程的更多相关文章

  1. 转:【Java并发编程】之四:守护线程与线程阻塞的四种情况

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17099981      守护线程   Java中有两类线程:User Thread(用户线 ...

  2. 【Java并发编程】之四:守护线程与线程阻塞的四种情况

    守护线程 Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) ​ 用户线程即运行在前台的线程,而守护线程是运行在后台的线程. 守护线程作用是为其他前台线程 ...

  3. java高并发系列 - 第9天:用户线程和守护线程

    守护线程是一种特殊的线程,在后台默默地完成一些系统性的服务,比如垃圾回收线程.JIT线程都是守护线程.与之对应的是用户线程,用户线程可以理解为是系统的工作线程,它会完成这个程序需要完成的业务操作.如果 ...

  4. 额!Java中用户线程和守护线程区别这么大?

    在 Java 语言中线程分为两类:用户线程和守护线程,而二者之间的区别却鲜有人知,所以本文磊哥带你来看二者之间的区别,以及守护线程需要注意的一些事项. 1.默认用户线程 Java 语言中无论是线程还是 ...

  5. java并发编程(四)守护进程 线程阻塞的四种情况

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17099981 守护线程   Java中有两类线程:User Thread(用户线程).Da ...

  6. Java用户线程和守护线程

    今天看Java一个关于多线程返回值方式的示例,发现一个自己不太能理解的问题,就是在主线程中启动了几个工作线程,主线程中也没有join,工作线程居然也是正常输出了回调的结果.这个跟linux C++下的 ...

  7. 【java多线程】用户线程和守护线程的区别

    java中线程分为两种类型:用户线程和守护线程.通过Thread.setDaemon(false)设置为用户线程:通过Thread.setDaemon(true)设置为守护线程.如果不设置次属性,默认 ...

  8. java 用户线程和守护线程

    在Java中通常有两种线程:用户线程和守护线程(也被称为服务线程)通过Thread.setDaemon(false)设置为用户线程通过Thread.setDaemon(true)设置为守护线程线程属性 ...

  9. JAVA笔记13__创建线程/线程休眠/等待线程终止/线程中断/守护线程

    /** * 线程:是进程的一个执行路径,共享一个内存空间,线程之间可以自由切换,并发执行,一个进程最少有一个进程(单线程程序) * 多线程两种实现方法:1.继承Thread类 2.实现Runnable ...

随机推荐

  1. 点击<a href="#">阻止自动跳转到顶部方法

    最近开发web项目,遇到一个问题 ,就是在<a>标签加href="#",并增加onclick事件,页面会自动在点击该标签绑定的元素时,自动跳转到页面顶部,在网上寻求了一 ...

  2. 【JavaWeb】Spring入门——HelloWorld

    0.为什么要使用Spring https://www.cnblogs.com/zmmi/p/7922186.html 1. 下载jar包 https://blog.csdn.net/qq_435401 ...

  3. ASP.NET Core搭建多层网站架构【13-扩展之支持全球化和本地化多语言】

    2020/02/03, ASP.NET Core 3.1, VS2019, ResXManager 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[13-扩展之支持全球化 ...

  4. 「CQOI2015」任务查询系统

    「CQOI2015」任务查询系统 传送门 好像也是板子题??? 区间修改,单点查询,考虑差分. 然后每次查询时就直接在对应的主席树上二分即可. 参考代码: #include <cstdio> ...

  5. /var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure world writable dir /home/python/.local in PATH, mode 040777 解决方案

    /var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure worl ...

  6. 4_2 刽子手游戏(UVa489)<自顶向下逐步求精法>

    Hangman Judge是一个猜英文单字的小游戏(在电子字典中常会看到),游戏规则如下:1.答案单字写在纸上(每个字元一张纸),并且被盖起来,玩家每次猜一个英文字元(letter).2.如果这个英文 ...

  7. Bugku-CTF加密篇之散乱的密文(lf5{ag024c483549d7fd@@1} 一张纸条上凌乱的写着2 1 6 5 3 4)

    散乱的密文 lf5{ag024c483549d7fd@@1} 一张纸条上凌乱的写着2 1 6 5 3 4  

  8. WLC-WLC升级(以2504为例)

    1.WLC升级需要按照升级路径来操作,低版本到高版本的跨度太大,往往需要升级到中间版本,有时候还涉及到FUS. 2.我们升级,一般使用的笔记本上运行的TFTP/FTP  server. 需要注意:笔记 ...

  9. 【译】高级T-SQL进阶系列 (二)【下篇】:使用 APPLY操作符

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 使用OUTER APPLY 操作符 OUTER APPLY操作符工作起来和CROSS APPLY比较类似.唯一的 ...

  10. ArrayStack(栈)

    顺序栈即数组型的栈.什么是栈呢?简单来说就像一个刚好装的下乒乓球大小的球筒,假设不能暴力打开球筒且只有一端有出口,那你放入或取出里面的球的操作都只能在一端进行,并且把球放进去或取出来都是由顺序决定的, ...