start()和run()的区别

start()方法:

        start()方法会启动多个线程,体现多线程的特性,start()方法会让不同线程中的run()方法里面的代码交替执行。

run()方法:

        run方法不会使不同线程中的run()方法里面的代码交替执行,代码还是同步执行,必须等待一个线程的run()方法里面的代码全部执行 完毕之后,另外一个线程才可以执行其run()方法里面的代码。

为什么我们调用 start() 方法时会执行 run() 方法,为什么我们不能 直接调用 run() 方法?
看看 Thread start 方法说明
/**
* 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 () {
......
}
        JVM执行 start 方法,会另起一条线程执行 thread run 方法,这才起到多线程的效果 ~
为什么我们不能 直接调用 run() 方法?
        如果直接调用 Thread run() 方法,其方法还是运行在主线程中,没有起到多线程效果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值