Java多线程3:Thread中start()和run()的区别

原文:http://www.cnblogs.com/skywang12345/p/3479083.html

start() 和 run()的区别说明
start():它的作用是启动一个新线程,新线程会执行相应的run()方法。start()不能被重复调用。
run():run()就和普通的成员方法一样,可以被重复调用。单独调用run()的话,会在当前线程中执行run(),而并不会启动新线程!

下面以代码来进行说明。

class MyThread extends Thread{
    public void run(){
        ...
    }
}
MyThread mythread = new MyThread();

mythread.start()会启动一个新线程,并在新线程中运行run()方法。
而mythread.run()则会直接在当前线程中运行run()方法,并不会启动一个新线程来运行run()。

start() 和run()的区别示例
下面,通过一个简单示例演示它们之间的区别。源码如下:

class MyThread extends Thread{
    public MyThread(String name) {
        super(name);
    }

    public void run(){
        System.out.println(Thread.currentThread().getName()+" is running");
    }
}

public class Demo {
    public static void main(String[] args) {
        Thread myThread=new MyThread("myThread");

        System.out.println(Thread.currentThread().getName()+" call myThread.run()");
        myThread.run();

        System.out.println(Thread.currentThread().getName()+" call myThread.start()");
        myThread.start();
    }
}

运行结果:

main call mythread.run()
main is running
main call mythread.start()
mythread is running

结果说明
(01) Thread.currentThread().getName()是用于获取“当前线程”的名字。当前线程是指正在cpu中调度执行的线程。
(02) mythread.run()是在“主线程main”中调用的,该run()方法直接运行在“主线程main”上。
(03) mythread.start()会启动“线程mythread”,“线程mythread”启动之后,会调用run()方法;此时的run()方法是运行在“线程mythread”上。

start() 和 run()相关源码
Thread.java中start()方法的源码如下:

/**
 * Causes this thread to begin execution; the Java Virtual Machine
 * calls the run method of this thread.
 * It is never legal to start a thread more than once.
 * In particular, a thread may not be restarted once it has completed
 * execution.*/
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();

说明:start()实际上是通过本地方法start0()启动线程的。而start0()会新运行一个线程,新线程会调用run()方法。

private native void start0();

Thread.java中run()的代码如下:

public void run() {
    if (target != null) {
        target.run();
    }
}

说明:target是一个Runnable对象。run()就是直接调用Thread线程的Runnable成员的run()方法,并不会新建一个线程。

上一篇:linux 禁止ping


下一篇:在linux中使用终端浏览器w3m