Thread类中start()方法和run()方法的区别

首先,通过继承Thread类的方式,创建一个线程,然后分别在主线程中调用run()和start()方法,观察两者输出的区别,代码如下:

package Demo;

/**
 * @ClassName:MyTheard
 * @Description:通过thread创建线程
 * @Author:legend Chan
 * @Date:2019/11/5 4:16 下午
 * @Version V1.0
 **/
public class ThreadCreate1 {
    public static void main(String[] args) {
        MyThread mt = new MyThread();
		//这里调用的是run()方法
        mt.run();

        System.out.println("这是主线程"+Thread.currentThread().getName());
    }
}
class MyThread extends Thread{
    @Override
    public void run() {

        for(int i = 0; i<10;i++){
            System.out.println("子线程"+this.getName()+"中的方法获取"+i);
        }

    }
}

上图中我们通过mt.run()来启动线程,接下来看输出结果:

Connected to the target VM, address: '127.0.0.1:65474', transport: 'socket'
子线程Thread-0中的方法获取0
子线程Thread-0中的方法获取1
子线程Thread-0中的方法获取2
子线程Thread-0中的方法获取3
子线程Thread-0中的方法获取4
子线程Thread-0中的方法获取5
子线程Thread-0中的方法获取6
子线程Thread-0中的方法获取7
子线程Thread-0中的方法获取8
子线程Thread-0中的方法获取9
这是主线程main

可以看出,程序是按照主线程的顺序执行的,当子线程执行完后,才打印主线程。

然后,我们更改程序,用mt.start()来启动线程,代码如下:

package Demo;

/**
 * @ClassName:MyTheard
 * @Description:通过thread创建线程
 * @Author:legend Chan
 * @Date:2019/11/5 4:16 下午
 * @Version V1.0
 **/
public class ThreadCreate1 {
    public static void main(String[] args) {
        MyThread mt = new MyThread();
        
        //这里调用的是start()方法
        mt.start();

        System.out.println("这是主线程"+Thread.currentThread().getName());
    }
}
class MyThread extends Thread{
    @Override
    public void run() {

        for(int i = 0; i<10;i++){
            System.out.println("子线程"+this.getName()+"中的方法获取"+i);
        }

    }
}

同样我们看运行之后的结果:

Connected to the target VM, address: '127.0.0.1:49473', transport: 'socket'
这是主线程main
子线程Thread-0中的方法获取0
子线程Thread-0中的方法获取1
子线程Thread-0中的方法获取2
子线程Thread-0中的方法获取3
子线程Thread-0中的方法获取4
子线程Thread-0中的方法获取5
子线程Thread-0中的方法获取6
子线程Thread-0中的方法获取7
子线程Thread-0中的方法获取8
子线程Thread-0中的方法获取9

不难发现,这次主线程的打印是在子线程执行之前,说明程序实现了异步。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值