The difference between thread start method and run method calls


1. Thread status

  1. NEWNew status
  2. Runnable ready state
  3. Running status
  4. blocking state
  5. state of death

2. The difference between directly calling the run method of a thread and calling the start method of a thread

1. Directly call the run method of the thread

当我们调用Thread线程run方法时,是没有开启额外线程,这里只是调用了一个普通方法,
线程先是就绪状态才能进入到运行状态,这里是主线程把它当成了一个普通方法来调用
而run方法真正的意义是当线程start后进入就绪状态后被调度或者cpu时间片执行run方法由就绪态进入运行态

		Thread thread = new Thread(){
    
    
            @Override
            public void run() {
    
    
                System.out.println("输出语句");
            }
        };
        thread.run();

2. Call the start method of the thread

		Thread thread = new Thread(){
    
    
            @Override
            public void run() {
    
    
                System.out.println("输出语句");
            }
        };
        System.out.println(thread.getState());//NEW新建状态
        thread.start();
        System.out.println(thread.getState());//Runnable就绪状态

Summarize

当我们调用Thread线程run方法时,是没有开启额外线程,这里只是调用了一个普通方法,
线程先是就绪状态才能进入到运行状态,这里是主线程把它当成了一个普通方法来调用
而run方法真正的意义是当线程start后进入就绪状态后被调度获得cpu时间片执行run方法由就绪态进入运行态

Guess you like

Origin blog.csdn.net/m0_50677223/article/details/130637065