1.Runnable:Runnable是一个接口,该接口中仅有一个方法为run方法,如下:
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();
}
2.Thread:Thread是实现了Runnable接口的方法,该类中有一个变量为
/* What will be run. */
private Runnable target;
Thread方法中有一个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();
/**
* 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();
}
}
当我们使用new Thread().start()启动一个线程的时候,其调用流程为start()->start0()->run(),我们可以注意到在start()方法中会调用start0()方法,start0()方法是一个native方法,它是怎么最后调用到run()方法的呢?
我们知道,当一个类第一次被使用到时,这个类的字节码会被加载到内存,并且只会回载一次。在这个被加载的字节码的入口维持着一个该类所有方法描述符的list,这些方法描述符包含这样一些信息:方法代码存于何处,它有哪些参数,方法的描述符(public之类)等等。
如果一个方法描述符内有native,这个描述符块将有一个指向该方法的实现的指针。这些实现在一些DLL文件内,但是它们会被操作系统加载到java程序的地址空间。当一个带有本地方法的类被加载时,其相关的DLL并未被加载,因此指向方法实现的指针并不会被设置。当本地方法被调用之前,这些DLL才会被加载,这是通过调用java.system.loadLibrary()实现的。
因此,Thread类编译完成后在虚拟机中运行的时候,带有native的方法会找到该方法的实现类,然后调用run()方法。
线程的两种创建方式
1 实现Runnable接口的方式
public class ThreadDemo implements Runnable{
@Override
public void run() {
System.out.println("start thread by Runnable");
}
}
2 继承Thread类的方式
public class ThreadDemo extends Thread{
@Override
public void run() {
System.out.println("start thread by Thread");
}
}
这两种方式有何不同?
上面我们说过Thread方法实现了Runnable接口,即实现了run()方法,当通过new Thread()方法来启动线程的时候,Thread会调用被重写的run()方法,当通过Runnable方法来启动的时候new Thread(Runnable target) ,在Thread构造的时候会把自己实现的Runnable类传递给Thread中的target变量,最后调用的时候即调用this.target.run()方法。实际上这里边用到了代理模式,Thread实际上是Runnable实现类的代理,当调用start方法的时候实际上最后还是调用的是我们自己重写的run()方法。
本文详细介绍了Java中创建线程的两种主要方式:实现Runnable接口与继承Thread类。通过源码解析了Thread类如何调用run方法,以及两种创建方式的具体区别。
1008

被折叠的 条评论
为什么被折叠?



