类的定义
public class Thread implements Runnable
初始化函数
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
init(g, target, name, stackSize, null, true);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals)
构造函数
- 无参
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
- 带一个参数
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
Runnable:线程回调后的目标对象
- 带一个参数,可以指定线程名称
/**
* Allocates a new {@code Thread} object. This constructor has the same
* effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
* {@code (null, null, name)}.
*
* @param name
* the name of the new thread
*/
public Thread(String name) {
init(null, null, name, 0);
}
- 带两个参数的构造函数
Thread(Runnable target, AccessControlContext acc) {
init(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
}
参数 | 说明 |
ThreadGroup group | 线程组 |
String name | 线程名 |
- 带三个参数的构造函数
public Thread(ThreadGroup group, Runnable target, String name) {
init(group, target, name, 0);
}
参数 | 说明 |
ThreadGroup group | 线程组 |
Runnable target | 线程回调后的目标对象 |
String name | 线程名 |
- 带四个参数的构造函数
参数 | 线程组 |
Runnable target | 线程回调后的目标对象 |
String name | 线程名 |
long stackSize | 线程堆栈大小 |
总结:Thread构造函数都是通过调用init函数进行初始化