转载:http://shmilyaw-hotmail-com.iteye.com/blog/1880902
前言
关于线程创建的问题,可以说是老生常谈了。在刚开始学习Thread的时候基本上都会接触到,用简单的一两句话就可以概括起来。一个是创建类实现Runnable接口,然后将该类的实例作为参数传入到Thread构造函数中。再调用Thread对象的start方法。还有一种是继承Thread类,覆写run方法。然后在该对象实例中调用start方法。那么,这两种方式在什么情况下适用呢?还有,既然我们要实现的类都要写run这个方法,为什么在构造的实例里要调用start方法呢?这多麻烦啊,还要绕一个弯,我直接调用run方法不行吗?这样岂不是更省事呢?对于这些问题,我会在本文中进行详细的分析和讨论。
典型的线程创建过程
这个过程可以说很简单,主要的两种方式如下:
一、 实现Runnable接口
主要三个步骤:
1. 在类中间实现Runnable接口
2. 创建该对象实例
3. 再新建一个Thread对象,将该对象作为参数传入构造函数
4. 调用Thread对象的start方法。
见代码如下:
Java代码
public class ThreadCon implements Runnable
{
public void run()
{
System.out.println("Entering the test thread...");
try
{
Thread.sleep(5000);
System.out.println("Thread executing...");
Thread.sleep(5000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ThreadCon con = new ThreadCon();
Thread thread = new Thread(con);
thread.start();
System.out.println("main thread");
}
}
二、继承Thread类
主要就是两个步骤:
1. 集成Thread类,实现run方法。
2. 创建该类的实例,并调用start()方法。
Java代码
public class ThreadCon extends Thread
{
public void run()
{
System.out.println("Entering the test thread...");
try
{
Thread.sleep(5000);
System.out.println("Thread executing...");
Thread.sleep(5000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ThreadCon thread = new ThreadCon();
thread.start();
System.out.println("main thread");
}
}
看了这两部分代码之后,不禁有几个问题。
1. 这两种创建线程的方式,在Thread类里面是怎么定义的呢?
2. 我们继承类或者实现接口,都要覆写run()这个方法,为什么后面要调用start方法呢?他们之间有什么关系呢?
我们先来看第一个问题。
线程相关类结构
如果我们去仔细看Thread类和Runnable接口的实现的话,他们会有这么一个关系:
Thread类本身就继承了Runnable接口。
Runnable接口本身的定义很简单:
Java代码
public interface Runnable {
/**
* When an object implementing interface Runnable
is used
* to create a thread, starting the thread causes the object's
* run
method to be called in that separately executing
* thread.
*
* The general contract of the method run
is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
Thread类中间定义的run方法实现了Runnable接口的规范:
Java代码
@Override
public void run() {
if (target != null) {
target.run();
}
}
上面这段代码里的target是一个Runnable的成员变量,在Thread里面的定义如下:
Java代码
/* What will be run. */
private Runnable target;
另外,再看到Thread类中间有一个如下定义的构造函数:
Java代码
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
我们就不难理解,我们通过构造一个实现Runnable接口的对象,将它作为参数传入Thread构造函数的原因了。这正好对应了我们第一种创建以及启动线程的过程。
对于第二种方式来说,当我们将一个类继承Thread的时候,通过覆写run方法,我们已经继承了Thread的定义,所以只要调用start方法就对应了我们第二种创建以及启动线程的过程。
写到这里的时候,我们会发现,还有一个遗漏了的地方。刚才只是讨论了我们要创建的对象和Runnable, Thread之间的关系。但是start方法到底怎么样,他们到底有什么关系呢?我们接下来很快就明白了。
start方法的秘密
我们来看看Thread里面start()方法的实现:
Java代码
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the run
method of this thread.
*
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* start
method) and the other thread (which executes its
* run
method).
*
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
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 */
}
}
}
private native void start0();
这部分的代码实际上并不长,这里主要的目的是调用了start0()这个方法。而start0这个方法在代码里的定义是一个private native的方法。这意味着什么呢?表示实际上这个start0方法是一个本地实现的方法。这个本地方法采用的不一定是java实现的,也可能是一个其他语言的实现,用来在JVM里面创建一个线程空间,再把run方法里面定义的指令加载到这个空间里。这样才能实现新建立一个线程的效果。所以,这也就是start方法能够创建线程的魔法之所在。
同时,这里也解释了为什么我们不能直接去调用run方法来期望它生成一个新的线程。如果我们直接调用run方法的话,它实际上就是在我们的同一个线程里面顺序执行的。在前面启动新线程的情况下,我们的代码执行结果会像如下的情况:
Java代码
main thread
Entering the test thread...
executing...
如果我们尝试将main函数里面的thread.start()改成thread.run(),我们会发现执行的结果如下:
Java代码
Entering the test thread...
Thread executing...
main thread
这种情况下run()方法和main函数其实是在同一个线程空间里执行。如果我们用一些profile工具进行分析的话,也会看到这样的场景。
两种创建线程方法的比较
通过继承的方式实现线程的方式用起来比较简单直接。但是从设计的角度来说,这个定义的类就需要和Thread方法有继承的关联关系。如果这个类本身又有其他的继承关系要处理呢?这个时候,这种方式就不是那么合适了。在Java本身的设计思路以及我们在面向对象设计思想的指导里,都不推荐优先考虑继承的关系。一个类可以实现多个接口,但是只能继承一个父类。所以说,采用实现Runnable接口的方式会是更加推荐的做法。
总结
对于线程的创建方式来说,实现接口的方法相对更加具有灵活性。因为这种方式不会和一个类建立固定的继承关系,可以更好的后续扩展一些。创建线程要调用start方法是因为start方法里面的一个特殊效果,它才能导致run方法的指令在新建的线程里执行。如果我们在建立实现runnable接口或者继承Thread对象的实例里直接调用run方法,这是不会产生新线程的。这也是为什么我们一定要调用start方法的原因。