目录
一. 属性一览
- name
- id
- thread group
- Runnable
- stack size
- daemon
二. 几个问题
- 为什么这些属性没有对应的setter(如:nama、thread group、Runnable、stack size)
- 为什么有些属性最好是构造器初始化,却没有在构造器初始化(如,daemon)
属性总览
属性 | 设置方式 | 是否必须 | 备注 |
---|---|---|---|
name | constructor | 是 | 如不写,会默认生成 |
id | 无 | 是 | 自动生成,仅能保证进程唯一 |
threadGroup | constructor | 是 | 如不写,采用当前父线程的 |
| public | 是 | |
name
对于线程而言,每个线程都必须有一个name,方便区分线程,但是name是可以重复!
- name必须有
- 如果不显示定义,那么方法会自动生成,其规则为
"Thread-" + nextThreadNum()
, - 值得注意的是, nextThreadNum是个synchronized方法,一定情况下会阻塞线程
public Thread(Runnable target) {//生成规则
init(null, target, "Thread-" + nextThreadNum(), 0);
}
private static int threadInitNumber;
private static synchronized int nextThreadNum() {//synchronized方法
return threadInitNumber++;
}
id
在init自动生成代码,仅仅保证在单进程唯一!
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
//some code ~
tid = nextThreadID();
}
private static synchronized long nextThreadID() {
return ++threadSeqNumber;
}
ThreadGroup
- threadGroup必须有
- 如果不显示定义则采用父类的threadGroup,
parent.getThreadGroup()
这里的parent 指的是当前线程,Thread parent = currentThread()
- start线程时,才会添加该thread到线程组内
Thread parent = currentThread()
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager
what to do. */
if (security != null) {//这就恶心了,什么高内聚低耦合,真是开发人员的想法,和java半毛钱关系没有
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();
}
}
public synchronized void start() {
//some code ~
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
//some code ~
}
- 方法(太多了,挑几个常用的)
1.获得当前ThreadGroup
Thread.currentThread().getThreadGroup()
2.获得当前ThreadGroupName
Thread.currentThread().getThreadGroup().getName()
3.复制copy当前线程组
Thread[] threads = new Thread[threadGroup.activeCount()];
threadGroup.enumerate(threads); // 这种会包含SubThreadGroup
threadGroup.enumerate(threads,false); // 这种不会包含SubThreadGroup
- 异常捕获
首先必须了解java线程的未捕获异常,JVM处理机制
方法:
1.设置 setDefaultUncaughtExceptionHandler
2.覆盖 ThreadGroup#uncaughtException方法,然后创建线程
线程一旦归入某个组就无法更换组
参考
ThreadGroup较全面的应用
ThreadGroup注释翻译
UncaughtExceptionHandler的应用,android - 奔溃日志收集
Runnable
- 非必须有
- 如果不定义的话,也可以用子类来覆盖run方法
stackSize
- 不必须有
- 虚拟机私有栈大小,注意大小不等同于次数,每次调用方法时,会压栈处理,栈会保存私有变量,入口和出口信息等
- 不一定有效,取决于JVM
- staticSize测试
二. 几个问题
1. 为什么有些属性没有对应的setter?(name,)
因为考虑到稳定性,线程创建后,如果这几个线程参数在代码中被修改,可能将大程度会影响线程稳定性!例如修改name,那么创建完后修改,可能就不好分辨线程了,所以这几个比较重要的参数,一旦创建Thread,就不能修改!