用ReentrantLock+Condition实现Blocking Queue。java
class TaskQueue{ final Queue<String> queue = new LinkedList<>(); final Lock lock = new ReentrantLock(); final Condition noEmpty = lock.newCondition(); public String getTask(){...} public void addTask(String name){...} }
Blocking Queue:当一个线程调用这个Queue的getTask()时,该方法内部可能让给线程变成等待状态,直到条件知足。线程被唤醒之后,getTask()才会返回。 而java.util.concurrent提供了线程安全的Blocking集合,如ArrayBlockingQueue就已经包含了一个线程安全的BlockingQueue。编程
当咱们使用BlockingQueue的时候,咱们的代码又被大大的简化了。take()是BlockingQueue提供的一个Blocking方法。当咱们调用take()方法的时候,在内部可能会被阻塞,直到条件知足线程才会返回。安全
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; class WorkerThread extends Thread{ BlockingQueue<String> taskQueue; public WorkerThread(BlockingQueue<String> taskQueue){ this.taskQueue = taskQueue; } public void run(){ while(!isInterrupted()){ String name; try{ name = taskQueue.take(); }catch (InterruptedException e){ break; } String result = "Hello,"+name+"!"; System.out.println(result); } } } public class Main{ public static void main(String[] args) throws Exception{ //在main方法中,咱们不须要本身编写一个BlockingQueue。咱们能够直接使用ArrayBlockingQueue,而后向Queue中添加3个任务 BlockingQueue<String> taskQueue = new ArrayBlockingQueue<>(1000); WorkerThread worker = new WorkerThread(taskQueue); worker.start(); taskQueue.put("Bob"); Thread.sleep(1000); taskQueue.put("Alice"); Thread.sleep(1000); taskQueue.put("Tim"); Thread.sleep(1000); worker.interrupt(); worker.join(); System.out.println("END"); } }
<img src="https://img2018.cnblogs.com/blog/1418970/201906/1418970-20190613123711859-1656906421.png" width="500" /> ## 3.java.util.concurrent提供了线程安全的Blocking集合 <img src="https://img2018.cnblogs.com/blog/1418970/201906/1418970-20190613125222851-1359401381.png" width="600" /> ## 4.java.util.Collections工具类 java.util.Collections工具类还提供了旧的线程安全集合转换器: 如把一个HashMap转化为一个线程安全的Map。实际上使用一个线程类,包装了非线程安全的map,而后对全部的读写方法都用synchronized加锁,这样得到了线程安全的集合。它的性能比Concurrent低不少,不推荐使用。。 ```#java Map unsafeMap = new HashMap(); Map threadSafeMap = Collections.synchronizedMap(unsafeMap); ```多线程
使用java.util.concurrent提供的Blocking集合能够简化多线程编程工具