用户线程和守护线程

用户线程

用户线程执行完,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

最新文章

  1. SQL Server数据库定时自动备份
  2. 打个酱油,欢迎指正FizzBuzzWhizz(c#)
  3. codeforce626C.Block Towers(二分)
  4. 【Python】Python XML 读写
  5. Eclipse SVN冲突解决
  6. docker基本概念,创建、起动实例,保存自定义镜像等常用操作
  7. wap网站获取访问者手机号PHP类文件
  8. Eclipse 中隐藏的 5 个非常有用的功能
  9. 更换Python默认软件镜像源
  10. 糟糠之妻下堂,娇俏公主上位——更换宝马三系座椅作业 - 切诺基 Jeep家族 越野e族论坛 越野/SUV/旅行/赛事/改装/互动中心
  11. I/O多路复用之select
  12. Android仿人人客户端(v5.7.1)——个人主页(三)
  13. Locked ownable synchronizers(转)
  14. 在小发现SQL字符串比较是不是他们的大写和小写敏感
  15. 抓包工具 - Fiddler(如何捕获Android数据包)
  16. 聊聊Vue.js组件间通信的几种姿势
  17. 机器学习基石12-Nonlinear Transformation
  18. linux -- 查看磁盘空间的大小
  19. html5-css渐变应用小实例,按钮
  20. Nginx特点及其配置

热门文章

  1. Fluent_Python_Part4面向对象,09-pythonic-obj,Python风格的对象
  2. Ajax请求状态200,却走error的函数
  3. WebApplicationInitializer初始化web应用,不需要web.xml
  4. 服务器(1)——IIS(1)——Windows7中IIS简单安装与配置(详细图解)
  5. Nexus-VDC(Virtual Device Context)
  6. [蓝桥杯2017初赛]k倍区间 前缀和
  7. Atcoder Grand Contest 039B(思维,BFS)
  8. python中的拷贝
  9. Ubuntu开启端口(持久化)
  10. 泛型和Object的区别?