多线程07:《疯狂Java讲义》学习笔记——线程组和未处理的异常
来源:互联网 发布:淘宝店身份认证复核 编辑:程序博客网 时间:2024/05/16 05:31
注:此文为学习《疯狂Java讲义》的笔记,因此内容全部来自于该书中。
1.线程组
ThreadGroup表示线程组,可以对一批线程进行分类管理。对线程组的控制相当于同时控制这批线程。用户创建的所有现场都属于指定线程组,如果没有指定,则线程数与默认线程组。默认情况下,子线程和创建它的父线程属于同一个线程组。线程运行中途不能改变它所属的线程组。
一旦某个线程加入了指定线程组之后,该线程将一直属于该线程组,直到该线程死亡,线程运行中不能改变它所属的线程组。
Thread类中相关构造方法。
Thread(
ThreadGroup group,
Runnable target)
Allocates a new Thread
object.
Thread(
ThreadGroup group,
Runnable target,
String name)
Allocates a new Thread
object so that it has target
as its run object, has the specified name
as its name, and belongs to the thread group referred to by group
.
Thread(
ThreadGroup group,
Runnable target,
String name, long stackSize)
Allocates a new Thread
object so that it has target
as its run object, has the specified name
as its name, and belongs to the thread group referred to by group
, and has the specified stack size.
Thread(
ThreadGroup group,
String name)
Allocates a new Thread
object.
ThreadGroup类的构造方法。
Constructor and Description
ThreadGroup(
String name)
Constructs a new thread group.
ThreadGroup(
ThreadGroup parent,
String name)
Creates a new thread group.
ThreadGroup方法。
Modifier and Type
Method and Description
int
activeCount()
Returns an estimate of the number of active threads in this thread group and its subgroups.
int
activeGroupCount()
Returns an estimate of the number of active groups in this thread group and its subgroups.
int
getMaxPriority()
Returns the maximum priority of this thread group.
void
interrupt()
Interrupts all threads in this thread group.
boolean
isDaemon()
Tests if this thread group is a daemon thread group.
boolean
isDestroyed()
Tests if this thread group has been destroyed.
void
setDaemon(boolean daemon)
Changes the daemon status of this thread group.
void
setMaxPriority(int pri)
Sets the maximum priority of the group.
void
uncaughtException(
Thread t,
Throwable e)
Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler
installed.
2.未处理的异常
如果线程执行过程中抛出了一个未处理异常,JVM在结束该线程之前自动查找是否有对应的UncaughtExceptionHandler对象,如果找到该处理器对象,调用该对象的uncaughtException(Thread t,Throwable e)方法处理异常。
Thread.UncaughtExceptionHandler是Thread类一个内部接口,只有一个方法,即voiduncaughtException()。
Thread类中设置异常处理器的方法如下。
Modifier and Type
Method and Description
static void
setDefaultUncaughtExceptionHandler(
Thread.UncaughtExceptionHandler eh)
Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.
void
setUncaughtExceptionHandler(
Thread.UncaughtExceptionHandler eh)
Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
ThreadGroup实现了Thread.UncaughtExceptionHandler接口。线程组会作为默认异常处理器。当一个线程抛出异常,JVM首先查找该异常对应的异常处理器,如果找到则调用该异常处理器处理该异常;否则JVM将会调用该线程组对象的uncaughtException()方法来处理该异常。
线程组处理异常的默认流程。(1)如果该线程组有父线程组,则调用父线程组的uncaughtException()方法来处理该异常。(2)如果该线程实例所属的线程类有默认的异常处理器,那么调用该异常处理器来处理该异常。(3)如果该异常对象时ThreadDeath的对象,则不做任何处理,否则,将异常跟踪栈的信息打印到System.err输出流,并结束该线程。
- 多线程07:《疯狂Java讲义》学习笔记——线程组和未处理的异常
- 多线程02:《疯狂Java讲义》学习笔记——线程的创建和启动
- 多线程03:《疯狂Java讲义》学习笔记——线程的生命周期
- 多线程01:《疯狂Java讲义》学习笔记——线程概述
- 多线程04:《疯狂Java讲义》学习笔记——控制线程
- 多线程05:《疯狂Java讲义》学习笔记——线程同步
- 多线程06:《疯狂Java讲义》学习笔记——线程通信
- 多线程08:《疯狂Java讲义》学习笔记——线程池
- 多线程09:《疯狂Java讲义》学习笔记——线程相关类
- 《疯狂Java讲义》——多线程学习
- java线程总结(一)—java疯狂讲义学习笔记
- 《疯狂Java讲义》——控制线程
- 《疯狂Java讲义》学习笔记
- 线程组和未处理的异常
- 线程组和未处理的异常
- Android疯狂讲义学习笔记——XML布局和java代码共同控制用户界面组件
- 疯狂JAVA讲义学习笔记3——数据类型和运算符
- 疯狂java讲义笔记——数据类型
- 【自定义标签开发】10-标签案例-开发转义标签
- 如何调整Linux内核启动中的驱动初始化顺序-驱动加载优先级
- Java中ArrayList类的用法(转)
- 第三方登陆步骤(友盟)
- jquery向网页输出的中文乱码的解决方法
- 多线程07:《疯狂Java讲义》学习笔记——线程组和未处理的异常
- 2把共享的属性方法移动到prototype上
- 【第七章】 对JDBC的支持 之 7.1 概述 ——跟我学spring3
- ASCII,Unicode和UTF-8字符编码详解
- 从一个简单例子 到 虚函数(摘抄)
- MFC多线程控制进度条
- 给DedeCms的验证码增加点击刷新功能
- 函数对象
- click在linksys wrt1200AC平台上的交叉编译