相似于计算机中,使用文件夹管理文件,也可使用线程组来管理线程,在线程组中定义一组类似的线程,在在线程组中也能够定义子线程组。java
Thread类有几个构造方法容许在建立线程时指定线程组,若是在建立线程时没有指定线程组,则该线程就属于父线程所在的线程组,JVM在建立main线程时会为它指定一个线程组,所以每一个java线程都有一个线程组与之相关,能够调用getThreadGroup()返回线程组吗。sql
public class ThreadGroupText { public static void main(String[] args) { ThreadGroup threadGroup=Thread.currentThread().getThreadGroup(); System.out.println(threadGroup); } }
public class ThreadGroupText { public static void main(String[] args) { ThreadGroup threadGroup1=new ThreadGroup("group1"); System.out.println(threadGroup1); } }
public class ThreadGroupText { public static void main(String[] args) { ThreadGroup threadGroup=Thread.currentThread().getThreadGroup(); System.out.println(threadGroup); ThreadGroup threadGroup1=new ThreadGroup("group1"); System.out.println(threadGroup1.getParent()); ThreadGroup threadGroup2=new ThreadGroup(threadGroup,"group2"); System.out.println("threadGroup1-->"+threadGroup1.getParent()); System.out.println("threadGroup2-->"+threadGroup2.getParent()); } }
public class ThreadGroupText { public static void main(String[] args) { Runnable r=new Runnable() { @Override public void run() { System.out.println(Thread.currentThread()); } }; Thread t1=new Thread(r,"t1"); System.out.println(t1); } }
Thread[t1,5,main]//线程名称 优先级 父线程组
在main线程中建立了t1线程,main为父线程,t1为子线程,t1没有指定线程组t1就属于父线程组。数组
activecount()返回当前线程组及子线程组中活动线程的数量ide
activeGroupCount()返回当前线程组以及子线程组中活动线程组的数量this
int enumerate(Thread[] list)将当前线程组中的活动线程复制到参数数组中spa
enmerate(ThreadGroup[] list) 将当前线程组中的活动线程复制到参数数组中线程
getMaxPriority()获取线程组最大优先级,默认是10设计
getParent()返回父线程组code
getName()返回线程组的名字blog
interrup()中断线程组的全部线程
IsDaemon()判断当前线程组是否为守护线程
list()将当前线程组中的活动线程打印出来
ParentOf(HtreadGroup g)判断当前线程组是否为参数线程组的父线程组
setDaemon()设置线程组为守护线程
在线程Run方法中,若是有受检异常必须捕获处理,若是想要得到Run方法中出现的运行时异常,能够经过回调UncaughtExceptHandler
接口得到哪一个线程出现了运行时异常。
getdefaultUncaughtExceptHandle
得到全局的UncaughtExceptHandler
getUncaughtExceptHandler
得到当前线程的UncaughtExceptHandler
setdefaultUncaughtExceptHandle
设置全局的UncaughtExceptHandler
setUncaughtExceptHandler
设置当前线程的UncaughtExceptHandler
当线程出现异常,JVM会调用Thread类的dispatchcaughtExceptHandler(Throwable e)
方法,该方法会调用
getUncaughtExceptHandler().UncaughtException(this e)
,若是想要得到异常信息,就须要设置线程的UncaughtExceptHandler
package com; public class ThreadExcept{ public static void main(String[] args) { //设置线程全局回调接口 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){ @Override public void uncaughtException(Thread t, Throwable e) { //t参数接受发生异常的线程,e就是该线程中的异常信息 System.out.println(t.getName()+"发生了"+e.getMessage()); } }); Thread t1=new Thread(new Runnable() { @Override public void run() { String name=null; System.out.println(name.length()); } }); t1.start(); } }
在实际开发中,这种设计异常处理方式仍是比较经常使用的,尤为是异常执行方法
若是线程产生异常,JVM会调用dispatchcaughtException
方法,该方法中调用了getUncaughtExceptionHandler().UncaughtException(this e)
;若是当前线程设置了UncaughtExceptionHandler
回调接口就直接调用它本身的UncaughtException
方法,若是没有设置则调用当前线程所在线程组UncaughtExceptionHandler
回调接口UncaughtException
方法,若是线程组也没有设置回调接口,则直接把异常的栈信息定向Sysytem.err中。
不少软件包括Mysql、Zookeeper、Kafka都存在Hook线程的效验机制,目的就是效验进程是否已启动,防止反复启动应用程序。
Hook线程也叫钩子线程,当JVM退出的时候会执行Hook线程,常常在程序启动的时候建立一个.lock线程,用.lock校验程序是否在启动,在程序退出时删除.lock文件,在Hook线程中处理防止重复启动以外还能够作资源释放,尽可能避免在Hook线程中作复杂操做。
package com; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class HookText { public static void main(String[] args) throws IOException, InterruptedException { //注入Hook线程,在程序退出时,删除.lock文件 Runtime.getRuntime().addShutdownHook(new Thread(){ @Override public void run() { System.out.println("JVM退出,会启动当前Hook线程,在Hook线程中删除.lock文件"); getFile().toFile().delete(); } }); //检查lock文件是否存在 if(getFile().toFile().exists()) { throw new RuntimeException("程序已启动"); } else { getFile().toFile().createNewFile(); System.out.println("建立lock文件"); } for (int i = 0; i < 100; i++) { System.out.println("程序正在运行"); Thread.sleep(100); } } private static Path getFile() { return Paths.get("","tmp.lock"); } }
当项目已启动,JVM会在项目文件夹目录会自动建立一个.lock文件
只有当项目自动运行结束JVM自动退出时会删除.lock文件,当让程序运行时中止,.lock文件不会被删除,再运行会抛出异常