JAVA中CountDownLatch的简单示例
public static void main(String[] args) throws InterruptedException { CountDownLatch latch =new CountDownLatch(10); for (int i = 0; i < 900; i++) { new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() ); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } finally { latch.countDown(); } } }).start(); } System.out.println("等待子线程运行结束"); latch.await(); System.out.println("子线程运行结束"); }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)