一道关于Thread的题

早上无事看到这个:

public static void main(String[] args)throws InterruptedException{
Thread t = new Thread(new MyThread2());
    t.start();
    System.out.print("main1");
    t.join();
    System.out.print("main2");
}

@Override
public void run() {
    System.out.print("run1");
    System.out.print("run2");
}

}

  • 因为join(),main2肯定最后打印;

  • run1 run2 打印顺序固定为:run1,run2;

  • 主要有点疑惑,main1,和run里面的到底谁先打印?

    run() method of
        * the receiver will be called by the receiver Thread itself (and not the
        * Thread calling start()).
        *
        * @throws IllegalThreadStateException - if this thread has already started.
        * @see Thread#run
        */
       public synchronized void start() {
           checkNotStarted();
    
       hasBeenStarted = true;
    
       nativeCreate(this,stackSize,daemon);

    }

看到start里的native开头,我就GG了。但是我知道,Thread的start不会阻塞主线程的运行,所以main1打印最先。

然后,敲代码多次运行验证:

G:\java>javac MyThread2.java

G:\java>java MyThread2
main1run1run2main2

相关文章

这个问题和curl无法访问https资源是类似的,现在curl可以访问https资源,但是使用pecl安装扩展的时候不行...
在浏览器输入chrome://flags/回车,找到Omnibox UI Hide Steady-State URL Scheme and Trivial Subdoma...
方法一: 我们都知道Ubuntu有一个专门用来安装软件的工具apt,我们可以用它来全自动安装arm-linux-gcc。...
中文的windows下的cmd默认使用GBK的编码,敲代码时,页面使用的是UTF-8(65001),而powershell控制台默认...
提示错误: arm-linux-gcc:Command not found PATH里有/usr/oca/arm/bin,但是make的时候,就是找不到 a...
我在Graph API开发中用的最多的测试工具就是Graph Explore,这个是微软开发的网页版的Graph API的测试工...