登录
    Technology changes quickly but people's minds change slowly.

线程执行顺序

技术宅 破玉 1076次浏览 0个评论

保证线程执行顺序:
原题目:

public static void main(String[] args) {
        System.out.println("start");
        new Thread(()-> System.out.println(1)).start();
        System.out.println(2);
        new Thread(()-> System.out.println(3)).start();
        System.out.println("end");
    }

改写上述代码以 start,1,2,3,end 的顺序执行
可以采用 join的方式:

public static void main(String[] args) throws InterruptedException {
        System.out.println("start");
        Thread t1=new Thread(()-> System.out.println(1));
        Thread t2=new Thread(()-> System.out.println(3));
        t1.start();
        t1.join();
        System.out.println(2);
        t2.start();
        t2.join();
        System.out.println("end");
    }

华裳绕指柔, 版权所有丨如未注明 , 均为原创|转载请注明线程执行顺序
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址