为什么主线程在Child Thread之前首先执行?

问题描述 投票:-5回答:1

通过调用t.start()...逻辑上它调用run方法并显示Child Thread ....但是为什么Main Thread在Child Thread之前首先执行?

class Mythread extends Thread
    {
    public void run()
    {
    for(int i=0;i<=5;i++)
    {
        System.out.println("child thread........");
    }
    }
    }

    public class ThreadDemo
    {
    public static void main(String arg[])
    {
    Mythread t=new Mythread();

    t.start();

    for(int i=0;i<=5;i++)
    {
        System.out.println("main thread........");
    }
    } 
    }
java multithreading
1个回答
1
投票

好吧,为什么不应该呢?

主线程已经在运行,调度程序可能更愿意让它继续运行。操作系统调度程序决定哪个线程运行多长时间。

没有规则线程应该在生成新线程后立即停止运行。由于创建新线程可能需要一段时间,因此起始线程将停止运行。相反,调度程序可以执行它认为最好的操作。

© www.soinside.com 2019 - 2024. All rights reserved.