一、前言
start( )方法是告诉CPU该Thread在可运行线程池准备就绪,等待线程调度,而run( )则是按顺序执行;
先看下面一个简单的列子
public void static main(String[] args)
Thread thread = new Thread(){
@Override
public void run() {
super.run();
run_start();
}
};
thread.start();
//thread.run();
System.out.println("a thread="+Thread.currentThread().getId());
}
private static void run_start() {
System.out.println("b thread="+Thread.currentThread().getId());
}
执行thread.start();输出结果
a thread 11
b thread 1
执行thread.run();输出结果
b thread 1
a thread 1
从结果可以看出start( )方法创建了thread11,真正实现了多线程运行,无需等待run方法体代码执行完而继续执行下面的代码,run( )方法是在本线程内调用该Runnable的run( )方法,当做Thread的普通方法的方式调用,程序依然在一个线程主线程中,按照顺序执行
二、源码分析
Thread类的run方法源码是这样的:target就是Runnable对象,因为Thread实现了Runnable接口,所以target.run( )还是在这个thread线程中执行,并没有开启新的线程,
/**
* 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的start( )方法源码是这样的
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.//线程执行,JVM会调用run()方法
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* 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".
*/
// Android-changed: throw if 'started' is true
if (threadStatus != 0 || started)//线程状态检验,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);
started = false;
try {
nativeCreate(this, stackSize, daemon);
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 */
}
}
}
从方法注释中了解到
1、线程执行,JVM会调用run()方法;
2、多次调用thread.start( )会报IllegalThreadStateException;
3、group.add(this)方法是将新建thread添加到ThreadGroup中
三、线程题目
class TaskBGThread extends Thread {
private int i=1;
public void run()
{
System.out.print("TaskBG" + i);
i++;
}
}
public class TaskThread implements Runnable {
private int i=1;
public void run()
{
System.out.print("Task" + i);
i++;
}
public static void main(String[] args) {
Runnable runner = new TaskThread();
Thread thTask1 = new Thread(runner);
thTask1.run();
Thread thTask2 = new Thread(runner);
thTask2.start();
Thread thTaskBG1 = new TaskBGThread();
thTaskBG1.start();
Thread thTaskBG2 = new TaskBGThread();
thTaskBG2.start();
}
程序输出结果是:Task1 Task2 TaskBG1 TaskBG1