一个系统中,若是线程数量不少,并且功能呢个分配比较明确,就能够将相同功能的线程放置在一个线程组里。打个比方,若是你有一本书,你就能够把它拿在手里,可是若是你有十本书,你就最好找一个书包,不然不方便携带。对于线程也是同等道理,若是你想处理十个或是上百个线程,最好仍是将他们都装进对应的书包里。java
线程组使用很是简单以下:ide
public class DiscoveryApplicaion { public static volatile int i = 0; static class Plustask implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getThreadGroup().getName()+"-"+Thread.currentThread().getName()); for (int k = 0; k < 1000; k++) { i++; } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { Thread[] task = new Thread[10]; ThreadGroup group = new ThreadGroup("Group1"); for (int j = 0; j <task.length ; j++) { task[j] = new Thread(group,new Plustask(),"T"+j); task[j].start(); task[j].join(); } System.out.println(group.activeCount()); group.list(); System.out.println(i); } }
线程组中有几个比较关键的方法,有助于你调试lua
注:强烈建议你们在建立线程组的时候,给取一个好听的名字。若是你在调试時拿到的是一堆Thread-0、Thread-1我想你们必定会抓狂的。spa
守护线程是一种特殊的线程,就和它的名字同样,他是系统的守护者,在后台默默的完成一些系统性的服务。好比垃圾回收线程、JIT线程就能够理解为守护线程。与之相对应的是用户线程,用户线程能够认为是系统的工做线程,它会完成这个程序应该要完成的业务操做。若是用户线程所有结束,这也意味着这个程序实际上无事可作了。守护线程要守护的对象已经不存在了,那么整个应用程序就天然应该结束。所以,当一个java应用内,只有守护线程時,java虚拟机就会天然退出。 操作系统
public class DiscoveryApplicaion { public static volatile int i = 0; static class Plustask implements Runnable{ @Override public void run() { while (true){ System.out.println("i am alive"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws InterruptedException { // SpringApplication.run(DiscoveryApplicaion.class, args); Thread aa = new Thread(new Plustask()); aa.setDaemon(true); aa.start(); Thread.sleep(2000); } }
这里要注意,设置守护线程必须在线程start()以前设置,不然你会获得如下异常,告诉你守护线程设置失败,但线程依然能够正常执行。只是被看成用户线程而已。线程
Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.setDaemon(Thread.java:1352) at Controller.DiscoveryApplicaion.main(DiscoveryApplicaion.java:28)
若是你不当心将setDaemon设置在了start()之后,那你就会诧异为何程序永远不会停下来呢。调试
优先级高的线程在竞争资源時会有更有优点,更可能抢占资源,固然这只是几率问题。若是运气很差,高优先级线程可能也会抢占失败。因为线程的优先级调度和底层操做系统有密切关系,在各个平台上表现不一,而且这种优先级产生的后果也可能不容易预测,没法精确控制。数字越大则优先级越高,但有效范围在1~10之间。code
public class DiscoveryApplicaion { public final static int MIN_PRIORITY = 1; public final static int NORM_PRIORITY = 5; public final static int MAX_PRIORITY = 10; static class HightPriority extends Thread{ static int count = 0; public void run() { while (true){ synchronized (DiscoveryApplicaion.class){ count++; if (count > 100000){ System.out.println("HightPriority 优先处理完成"); break; } } } } } static class LowPriority extends Thread{ static int count = 0; public void run() { while (true){ synchronized (DiscoveryApplicaion.class){ count++; if (count > 100000){ System.out.println("LowPriority 优先处理完成"); break; } } } } } public static void main(String[] args) throws InterruptedException { Thread hight = new HightPriority(); Thread low = new LowPriority(); low.setPriority(MAX_PRIORITY); hight.setPriority(MIN_PRIORITY); low.start(); hight.start(); } }
能够尝试执行上述代码,能够看到,高优先级的线程在大部分状况下,都会首先完成任务。(但这不能确保全部状况下,必定都是这样)对象