Thread和ThreadGroup
学习了:https://www.cnblogs.com/yiwangzhibujian/p/6212104.html 这个里面有Thread的基本内容;
http://blog.csdn.net/a352193394/article/details/39323427 这个讲解了ThreadGroup
https://www.cnblogs.com/yy2011/archive/2011/05/05/2037564.html 这个比较了ThreadGroup与Executor
Thread比如属于一个ThreadGroup,
源码:
Thread parent = currentThread(); SecurityManager security = System.getSecurityManager(); if (g == null) { // 这个就是ThreadGroup /* Determine if it's an applet or not */ /* If there is a security manager, ask the security manager what to do. */ if (security != null) { g = security.getThreadGroup(); } /* If the security doesn't have a strong opinion of the matter use the parent thread group. */ if (g == null) { g = parent.getThreadGroup(); } }
Thread.setUncaughtExceptionHandler的意义在于不能修改原线程的情况下,为其增加一个错误处理器。
package com.stono.thread2; import java.lang.Thread.UncaughtExceptionHandler; // UnCaughtException意义在于不能对原线程修改的情况下,为其增加一个错误处理器。 public class ThreadUncaughtException { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { public void run() { int a = 1/0; } }); t1.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("线程:"+t.getName()+"出现异常,异常信息:"+e); e.printStackTrace(); } }); t1.start(); } }
ThreadGroup.enumerate(list)方法把ThreadGroup中的线程拷贝到新的List中;
在ThreadGroup讲解过程中,一个runnable对象构建了多个线程,这个需要注意一下,TODO