目录
初始化
线程启动
join
interrupt
//初始化的时候,线程名称 "Thread-全局递增数字"init(null, target, "Thread-" + nextThreadNum(), 0);private static int threadInitNumber;
private static synchronized int nextThreadNum() {return threadInitNumber++;
}
小结:
public synchronized void start() {//这里的判断是防止线程多次启动, 一旦启动 threadStatus就不在是0if (threadStatus != 0)throw new IllegalThreadStateException();group.add(this);boolean started = false;try {start0();started = true;} finally {try {if (!started) {group.threadStartFailed(this);}} catch (Throwable ignore) {/* do nothing. If start0 threw a Throwable thenit will be passed up the call stack */}}
}
小结
interrupt打断一个线程,其实是在修改那个线程里的一个interrupt的标志位,打断他以后,interrupt标志位就会变成true,所以在线程内部,可以根据这个标志位,isInterrupted这个标志位来判断,是否要继续运行
并不是说,直接interrupt一下某个线程,直接就不让他运行了
还有一个更加常见的用法,就是说什么呢?打断一个线程的休眠
上一篇:C++(中级+高级部分)