Java 模拟并发操作

来源:互联网 发布:淘宝义卖作文 编辑:程序博客网 时间:2024/06/10 20:57
public class Test {public static CountDownLatch cdl = new CountDownLatch(10);public static int count = 10;public static void main(String[] args) {for (int i = 1, length = (int) cdl.getCount(); i <= length; i++) {cdl.countDown();new Thread(() -> {try {// 等待线程初始完毕cdl.await();} catch (Exception e) {e.printStackTrace();}// Do somethingSystem.out.println("I got " + count);count--;}).start();}}}

运行结果: