java - java线程顺序
问题描述
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> System.out.println("thread1"));
Thread thread2 = new Thread(() -> System.out.println("thread2"));
Thread thread3 = new Thread(() -> System.out.println("thread3"));
Thread thread4 = new Thread(() -> System.out.println("thread4"));
Thread thread5 = new Thread(() -> System.out.println("thread5"));
thread5.start();
thread3.start();
thread1.start();
thread3.join();
thread2.start();
thread1.join();
thread4.start();
thread2.join();
thread4.join();
thread5.join();
}
我认为这次运行的结果将是随机的。但是,5、3 和 1 始终排在第一位。
结果1
thread3
thread1
thread5
thread2
thread4
结果2
thread3
thread1
thread5
thread2
thread4
结果3
thread5
thread1
thread3
thread2
thread4
始终执行 5,1,3,然后执行 2,4。为什么会有订单?
解决方案
推荐阅读
- node.js - 无法运行“npm start”ReactJS
- azure-sql-database - SQL 身份验证 Windows 应用程序
- c# - 这段代码会导致内存泄漏吗?
- python - 使用另一个脚本调用脚本时出现导入问题
- php - 将数组转换为字符串或对象
- java - Spring Boot+Thymeleaf each+if
- javascript - java - 如何在java脚本中解决连续警报框?
- java - 用于创建 JasperReport 后未释放池连接
- python - 如何将 split() 与 float() 结合使用
- apache - 带有子目录的 Apache 重定向