【多线程】——线程中run方法和start方法的区别

线程中run方法和start方法的区别

第一个区别

  • run方法是普通方法,并不会新创建线程。
  • start方法会创建一个新线程去执行。

run方法

public class ThreadDemo12 {

    public static class MyThread extends Thread{
        @Override
        public void run(){
            System.out.println("我是线程1"+
                    Thread.currentThread().getName());
        }
    }
    public static void main(String[] args) {
        MyThread thread=new MyThread();
        //相当于调用一个类的普通方法而已,并不是创建了新线程执行的
       thread.run();
    }
}

在这里插入图片描述
start方法

public class ThreadDemo12 {

    public static class MyThread extends Thread{
        @Override
        public void run(){
            System.out.println("我是线程1"+
                    Thread.currentThread().getName());
        }
    }
    public static void main(String[] args) {
        MyThread thread=new MyThread();
        //相当于创建了一个新线程并且执行了run方法
        thread.start();
    }
}

在这里插入图片描述

第二种区别

  • run方法可以多次调用
  • start方法不可以多次调用

run方法

public class ThreadDemo12 {

    public static class MyThread extends Thread{
        @Override
        public void run(){
            System.out.println("我是线程1"+
                    Thread.currentThread().getName());
        }
    }
    public static void main(String[] args) {
        MyThread thread=new MyThread();
        //相当于创建了一个新线程并且执行了run方法
        //thread.start();
        //相当于调用一个类的普通方法而已,并不是创建了新线程执行的
        thread.run();
        thread.run();
    }
}

在这里插入图片描述
start方法

public class ThreadDemo12 {

    public static class MyThread extends Thread{
        @Override
        public void run(){
            System.out.println("我是线程1"+
                    Thread.currentThread().getName());
        }
    }
    public static void main(String[] args) {
        MyThread thread=new MyThread();
        //相当于创建了一个新线程并且执行了run方法
        thread.start();
       thread.start();
    }
}

在这里插入图片描述

第三种区别

  • run方法属于自定义的实现类
  • start方法属于 Thread 类

(经典面试题)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值