启动线程用start方法而不是用run方法

public static void main(String[] args) {
        
        Thread t=new Thread("Thread-TEST"){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        };
        t.start();
        // t.run();
    }

 

start 方法会调用start0方法,start0是native方法,会启动一个线程,由虚拟机去调用线程的run方法,这里由于重写了父类的run方法,所以调用子类的run方法

用start启动线程,打印的是

启动线程用start方法而不是run方法

 

说明创建了新的线程,线程名字是Thread-TEST

 

 

用run方法时

启动线程用start方法而不是run方法

 

 说明这时候,是直接在主线程里执行的,没有创建线程,只是调用了对象的方法而已,因为没有调用start0本地方法

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-20
  • 2021-06-17
  • 2022-12-23
  • 2022-01-16
猜你喜欢
  • 2021-11-28
  • 2022-12-23
  • 2021-07-01
  • 2021-08-24
  • 2021-08-13
  • 2022-12-23
  • 2021-11-16
相关资源
相似解决方案