Thread中的run()和start()的区别

啥都不用说,先上一个例子:

/**
 * @author I321023
 * different between function run and function start in class Thread
 */
public class TestRS extends Thread{
    public TestRS(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("Thread is running~");
    }

    public static void main(String[] args) {
        TestRS tRs = new TestRS("My thread");
        tRs.run();//Thread is running~
        tRs.start();//Thread is running~
    }
}

对于上面这个简单的程序的输出结果大家应该都没有疑问,但是应该发现run(),start()最终的结果都是调用了run()方法,那他两是不是一样的呢?再来看一段代码:

/**
 * @author I321023
 * 
 * different between function run and function start in class Thread
 */
public class TestRS extends Thread{
    public TestRS(String name) {
        super(name);
    }

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

    public static void main(String[] args) {
        TestRS tRs = new TestRS("My thread");
        tRs.run();//main is running!
        tRs.start();//My thread is running!
    }
}

这里大家可以清楚的看到这两个的区别,下面总结一下:
1. tRs.run();//main is running!是在主线程main中调用的,并没有新线程
2. tRs.start();//My thread is running!start()方法会新启动一个线程,启动后,这个新线程会调用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是Runnable对象,如果不为空直接调用Runnable的成员方法run(),并不会新建线程
            target.run();
        }
    }
/**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <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".
         */
        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();//调用本地方法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();//会新开一个线程,新线程再调用run()方法

PS:从上面两段源码,大家应该可以很清楚看到run()和start()的区别,当多线程的时候千万不要弄错了。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值