1、Thread start方法源码分析及注意事项
2、模板设置模式在Thread中的应用
3、Thread模拟营业大厅叫号机程序
本篇博客将分析Thread的start方法,在调用start方法之后到底进行了什么操作,通过之前的了解我们知道了,start方法启动了一个线程,并且该线程进入了可执行状态(Runnable),在之前的案例中我们重写了Thread的run方法,但却调用了start方法,那么run方法和start方法有什么关系呢?带着疑问 我们来寻找答案。
-------------------------------------------------Thread start方法源码分析及注意事项---------------------------------------
先看一下Thread类
public class Thread implements Runnable {
显然Thread类实现了Runnable接口,那么我们再看看runnable接口市是什么样子的,源码如下。
/*
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
/**
……
*
* @author Arthur van Hoff
* @see java.lang.Thread
* @see java.util.concurrent.Callable
* @since JDK1.0
*/
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
由以上源码可以看出来runnable接口仅仅有一个抽象的 run方法。
那么接下来我们看看,start方法的具体实现
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* 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);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
- synchronized 同事只有一个执行模块可以进入
if (threadStatus != 0) throw new IllegalThreadStateException();
这是什么意思呢?如果线程的一个属性threadStatus不为0就报错了。
我们可以看到源码,如下:
private volatile int threadStatus = 0;
线程的初始化的时候这个状态threadStatus为0,意思就是我们对这个线程进行start的时候状态就不是0了来防止线程的重复被start。
3. group.add(this);
当线程状态为0的时候,我们把这个线程放在一个group里面,我们来看看这个group是干嘛的?
源码 如下我们看到:
/* The group of this thread */
private ThreadGroup group;
其实group就是一个线程组,
我们大致分析一下这个线程组:
线程组类的源码如下,实现了一个线程捕获异常的监听接口
public class ThreadGroup implements Thread.UncaughtExceptionHandler {
我们在看看线程组的add方法:
void add(Thread t) {
//并发
synchronized (this) {
//线程状态,是否被销毁,一旦被销毁则报错
if (destroyed) {
throw new IllegalThreadStateException();
}
//线程数组
if (threads == null) {
//初始化长度为4
threads = new Thread[4];
//检查线程数组是否已满
} else if (nthreads == threads.length) {
//扩容
threads = Arrays.copyOf(threads, nthreads * 2);
}
//把线程放在线程组里面
threads[nthreads] = t;
// This is done last so it doesn't matter in case the
// thread is killed
//下表+1
nthreads++;
// The thread is now a fully fledged member of the group, even
// though it may, or may not, have been started yet. It will prevent
// the group from being destroyed so the unstarted Threads count is
// decremented.
//没有启动的线程数
nUnstartedThreads--;
}
}
- 继续看源码调用了**start0();**方法
看了一下源码,如下:
private native void start0();
start0();启动线程。其实最核心的就是部分就是start0()这个本地方法,也就是JNI方法。源码如上。
也就是说start方法会用到start0 方法,那么重写的那个run方法何时被调用的呢?但从上面是看不出任何端倪的,但是JDK的官方文档,在start方法中又如下的注释说明:
在开始执行这个线程时,JVM将会调用该线程的run方法,换言之,run方法是被JNI方法start0()调用的。
仔细研究start源码得出一下几点
(1)Thread被构造后的NEW状态,事实上threadStatus这个内部属性为0
(2)不能两次启动Thread,否则就会出现IllegalThreadStateException异常。
(3)线程启动后将会被加入一个ThrreadGroup中
(4)一个线程的生命周期结束,也就是到了terminated状态,再次调用start方法是不允许的,也就是说terminated状态是没有办法回到runnable/running状态的
第一个例子重复启动
//main
public static void main(String [] args){
Thread t = new Thread(){
@Override
public void run(){
browseNews();
}
};
t.start();
t.start();
// new Thread(TestThread::enjoyMusic).start();
}
报错信息如下:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at thread.test.TestThread.main(TestThread.java:19)
线程禁止重复启动。
第二个例子,terminated线程不能再次启动
//main
public static void main(String [] args) throws InterruptedException {
Thread t = new Thread(){
@Override
public void run(){
// browseNews();
System.out.println("terminated线程不能再次启动");
}
};
t.start();
TimeUnit.SECONDS.sleep(3);//为了让线程t进入 terminated状态
t.start();//再次启动线程
// new Thread(TestThread::enjoyMusic).start();
}
运行结果如下:
terminated线程不能再次启动
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at thread.test.TestThread.main(TestThread.java:24)
虽然这两个异常抛出的一样,但是这两个异常有本质的区别,第一个是重复启动,只是第二次启动是不允许的但是之前启动的线程是运行状态,而第二个案例是企图重新激活也抛出了非法状态的异常,但是此时没有线程,因为线程的生命周期已经结束了。
--------------------------------------------------------模板设置模式在Thread中的应用---------------------------------------
不难看出,线程的真正的执行逻辑式在run方法中,通常我们会把run方法称为线程的执行单元,这也就回答了我们最开始的疑问,重写run方法,用start方法启动线程。thread中run方法代码如下,如果我们没有使用runnable接口对其进行构造,则可以认为thread的run方法本社就是一个空的实现:
源码:
/**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}
其实thread的run和start就是一个比较典型的模板设计模式,父类编写算法结构代码,子类实现逻辑细节,下面通过一个简单的例子来看一下模板设计模式,再参照该模式在Thread中使用,代码如下:
package thread.test;
public class MMTest {
public final void print(String msg){
System.out.println("******************");
wprintlv(msg);
System.out.println("******************");
}
protected void wprintlv(String msg){
}
public static void main(String args[]){
MMTest t1 = new MMTest(){
@Override
protected void wprintlv(String msg){
System.out.println(msg);
}
};
t1.print("哈哈哈,模板模式就是这么简单");
MMTest t2 = new MMTest(){
@Override
protected void wprintlv(String msg){
System.out.println(msg);
}
};
t2.print("222哈哈哈,模板模式就是这么简单");
}
}
测试输出结果如下:
******************
哈哈哈,模板模式就是这么简单
******************
******************
222哈哈哈,模板模式就是这么简单
******************
可以看书print方法类似于Thread的start方法,而wprintlv则类似于run方法,这样做的好处是,程序机构由父类控制,并且是final修饰符,不允许重写,子类只需要实现想要的逻辑任务就可以。
--------------------------------------------------------Thread模拟营业大厅叫号机程序---------------------------------------
可以试着写一下,代码随后贴出来。