首页 > 解决方案 > java中调用thread.start()后的语句流程是什么?

问题描述

在 thread.start() 之后执行什么语句。主线程的语句或子线程的语句。

线程通常并行运行,但在这种情况下,主线程的语句始终优先于子线程的语句。真正的原因或流程是什么?

public class Test extends Thread { 
    public static void main(String[] args) { 
        ChildThread ct = new ChildThread(); 
        ct.start();
        System.out.println("main"); 
    } 
} 
class ChildThread extends Thread{ 
    @Override public void run() {       
        System.out.println("Child thread"); 
    } 
}

标签: javamultithreading

解决方案


没有确定的答案是什么语句在 thread.start() 之后执行,但在大多数情况下它将是主线程,因为当你启动新线程时,你的操作系统需要初始化这个线程,而主线程已经在飞。


推荐阅读