main方法里面写了个线程start,结果每次都是main的先顺序执行完成?

说来挺傻的,写了个new Thread().start()就没想到,在没有执行到start那一步之前,还是走的单线程啊,顺序执行下来,你start方法写在后面当然就后执行啊。

然后把start往前一提,就变成交叉执行输出了。

package com.thread.simple;

public class ThreadOne extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 10; i ++) {
            System.out.println("sub" + i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new ThreadOne();
        thread.start();
        for (int i = 0; i < 10; i ++) {
            System.out.println("main--" + i);
//            Thread.sleep((int) (Math.random() * 1000));
        }


    }
}

 

posted on 2020-10-02 08:24  我欲皆真  阅读(615)  评论(0编辑  收藏  举报

导航