多线程——start()和run()

线程的创建和启动,这篇博文主要讨论一下start()和run()这两个方法。

1. run()方法

线程的创建和启动的总结中有说过:根据java api,Thread类本身也是实现了Runnable接口,有:

public class Thread extends Object implements Runnable

现在来看一下Runnable接口,根据java api,Runnable接口只有一个方法,就是run():

这里写图片描述

“When an object implementing interface Runnable is used to create a thread, starting the thread causes the object’s run method to be called in that separately executing thread.”

“当通过实现Runnable接口来创建线程时,启动线程会使得run()方法在那个独立执行的线程中被调用。”

2. start()方法

根据java api,start()方法是Thread的类方法:

这里写图片描述

“Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.”
“start()方法会使得该线程开始执行;java虚拟机会去调用该线程的run()方法。”

因此,t.start()会导致run()方法被调用,run()方法中的内容称为线程体,它就是这个线程需要执行的工作。

用start()来启动线程,实现了真正意义上的启动线程,此时会出现异步执行的效果,即在线程的创建和启动中所述的随机性。‘
而如果使用run()来启动线程,就不是异步执行了,而是同步执行,不会达到使用线程的意义。


代码对比:

1.通过t.start()启动线程,JVM会自动调用该线程的run()方法

/*
 * 通过实现Runnable接口创建线程
 */
public class MyThread_runnable implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+": Hello");
    }

    public static void main(String[] args) {
        for(int i=0; i<10; i++){
            System.out.println("创建一个线程");
            MyThread_runnable r = new MyThread_runnable();
            Thread t = new Thread(r);
            System.out.println("启动线程");
            t.start();
        }   
    }
}

运行结果:

这里写图片描述

2.通过t.run()调用run()方法

public class MyThread_runnable implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+": Hello");
    }

    public static void main(String[] args) {
        for(int i=0; i<10; i++){
            System.out.println("创建一个线程");
            MyThread_runnable r = new MyThread_runnable();
            Thread t = new Thread(r);
            System.out.println("启动线程");
            t.run();
        }   
    }
}

运行结果:

这里写图片描述

总结:

  1. 从运行结果肯明显可以看出,使用start()方法具有异步执行的效果,而使用run()方法是同步执行的效果,运行结果中规中矩。
  2. 使用start()方法,是真的启动了相应的线程0-9,而使用run()方法并没有真的启动线程,而是由一个叫main的主线程去调用的run()方法。
  3. 所以,正确启动线程的方式是使用start()方法。

这里写图片描述

  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值