Java线程中 start与run的区别以及源码分析



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()相关源码(基于JDK1.7.0_80)

Thread.javastart()方法的源码如下:

public synchronized void start() {

    // A zero status value corresponds to state "NEW".如果线程不是"NEW",则抛出异常!

    if (threadStatus != 0)

        throw new IllegalThreadStateException();

 

    // 将线程添加到ThreadGroup中,底层实现:Thread threads[]数组;

    group.add(this);

 

    boolean started = false;

    try {

        // 通过start0()启动线程     

   start0();//private native void start0();查看open jdk

        // 设置started标记

        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 */

        }

    }

}

   ThreadGroup中的add():

 void add(Thread t) {

        synchronized (this) {

//判断线程是否销毁

            if (destroyed) {

                throw new IllegalThreadStateException();

            }

//如果线程组为空,则新建一个大小为4的线程组

            if (threads == null) {

                threads = new Thread[4];

            } else if (nthreads == threads.length) {

                //nthreads我的理解是线程组中的线程数,当nthreads == threads.length时,就表示已经有8个线程了,再有线程加进来时threadgroup就不够放了,就得扩容,2倍扩容类似于arraylist的扩容方式。

                threads = Arrays.copyOf(threads, nthreads * 2);

            }

            threads[nthreads] = t;

 

            // This is done last so it doesn't matter in case the

            // thread is killed不太理解,线程被杀死的操作是什么?本来被放到线程组的线程被杀死后,nthreads还要++??

            nthreads++;

 

            // The thread is now a fully fledged member of the group, even

            // though it may, or may not, have been started yet. It will prevent

            // the group from being destroyed so the unstarted Threads count is

            // decremented.

            nUnstartedThreads--;

        }

    }




Thread.javarun()的代码如下:

 

 

public void run() {

    if (target != null) {

        target.run();

    }

}

mythread.run()--Threa.Class里的run--Runnable对象(还是回到mythread)的run

 

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




to be continued...



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值