线程分组 | 守护线程 | 线程优先级
来源:互联网 发布:英雄美工钢笔9076 编辑:程序博客网 时间:2024/06/02 10:56
线程分组
线程分组可以理解为一些线程的分类。在多线程的程序当中,在调试或者打印日志的时候,众多的线程一般很难区分开来。但是通过线程的groupName和threadName可以清晰的看出是哪个线程。但是首先你需要给线程或线程组命一个清晰明了的名字。
Java中的线程优先级是从1-10之间的。当我们创建一个线程时,默认的优先级是5(NORM_PRIORITY)。在Thread类中也有以下常量定义:
链接:http://moguhu.com/article/detail?articleId=25
线程分组可以理解为一些线程的分类。在多线程的程序当中,在调试或者打印日志的时候,众多的线程一般很难区分开来。但是通过线程的groupName和threadName可以清晰的看出是哪个线程。但是首先你需要给线程或线程组命一个清晰明了的名字。
下面上一个demo:
public class ThreadGroupName implements Runnable {public static void main(String[] args) {ThreadGroup tg = new ThreadGroup("PrintGroup");Thread t1 = new Thread(tg, new ThreadGroupName(), "T1");Thread t2 = new Thread(tg, new ThreadGroupName(), "T2");t1.start();t2.start();System.out.println(tg.activeCount());tg.list();}@Overridepublic void run() {String groupAndName = Thread.currentThread().getThreadGroup().getName() + "-"+ Thread.currentThread().getName();while (true) {System.out.println("I am " + groupAndName);try {Thread.sleep(3000);} catch (Exception e) {e.printStackTrace();}}}}
守护线程
从名字就可以看出来,这一类线程是为了某些线程而存在的。
守护线程通常为一些后台服务性的线程,如:GC线程、JIT线程等。这些线程在普通线程退出后,自动结束生命周期。
对于守护线程的创建,比普通线程多的一个操作就是:在线程启动之前,设置setDaemon(true)就可以了。demo如下:
public class DaemonDemo {public static class DaemonT extends Thread {@Overridepublic void run() {while (true) {System.out.println("I am alive");try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}}}}public static void main(String[] args) throws InterruptedException {Thread t = new DaemonT();t.setDaemon(true);t.start();Thread.sleep(2000);}}线程优先级
Java中的线程优先级是从1-10之间的。当我们创建一个线程时,默认的优先级是5(NORM_PRIORITY)。在Thread类中也有以下常量定义:
/** * The minimum priority that a thread can have. */ public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ public final static int MAX_PRIORITY = 10;对于线程设置的优先级,优先级高的并不一定在优先级低的之前执行。只是说让其执行的可能性更大一些。
public class PriorityDemo {public static class HightPriority extends Thread {static int count = 0;@Overridepublic void run() {while(true) {synchronized (PriorityDemo.class) {count++;if(count > 10000000) {System.out.println("HightPriority is completed");break;}}}}}public static class LowPriority extends Thread {static int count = 0;@Overridepublic void run() {while(true) {synchronized (PriorityDemo.class) {count++;if(count > 10000000) {System.out.println("LowPriority is completed");break;}}}}}public static void main(String[] args) {HightPriority high = new HightPriority();LowPriority low = new LowPriority();high.setPriority(Thread.MAX_PRIORITY);low.setPriority(Thread.MIN_PRIORITY);low.start();high.start();}}
链接:http://moguhu.com/article/detail?articleId=25
阅读全文
1 0
- 线程分组 | 守护线程 | 线程优先级
- ava多线程系列-- 之 线程优先级和守护线程
- Java多线程基础--10之 线程优先级和守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 守护线程
- 【c#】C#调用matlab动态链接库
- 2017多校4 1011 Time To Get Up
- HDUOJ Let the Balloon Rise
- 解决浏览器会产生缓存的问题
- POJ 2513 Colored Sticks 好题 字典树+并查集+欧拉路
- 线程分组 | 守护线程 | 线程优先级
- Arrays静态方法
- SDPhotoBrowser 详细介绍 非常简单易用的图片浏览器,模仿微博图片有源码例子
- C++实现俄罗斯方块源码分享(Windows编程),有图形界面
- linux 中IO多路复用epoll函数的ET和LT工作模式详解
- 会场预约
- 递归转动态规划
- ContextLoaderListener作用详解
- java常用类的方法总结---String类