Java多线程之CountDownLatch用法

CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。 主要方法 public CountDownLatch(int count); public void countDown(); public void await() throws InterruptedException 构造方法参数指定了计数的次数 countDown方法,当前线程调用此方法,则计数减一 awaint方法,调用此方法会一直阻塞当前线程,直到计时器的值为0   [java lang="java"] public class CountDownLatchDemo { final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws InterruptedException { CountDownLatch latch=new CountDownLatch(2);//两个工人的协作 Worker worker1=new Worker("zhang san", 5000, latch); Worker worker2=new Worker("li si"...

java多线程之BlockingQueue

[java lang="java"] package com.wilson.jdk5current; import java.io.File; import java.io.FileFilter; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; public class TestBlockingQueue { static long randomTime() { return (long) (Math.random() * 1000); } public static void main(String[] args) { // 能容纳100个文件 final BlockingQueue<File>; queue = new LinkedBlockingQueue<File>(100); // 线程池 final ExecutorService exec = Executors.newFixedThreadPool(5); final File root = new File("C:\\log"); // 完成标志 ...