阻塞队列BlockingQueue
来源:互联网 发布:手机视频点播软件 编辑:程序博客网 时间:2024/06/14 09:10
1.知识点
2.用阻塞队列实现生产者消费者的例子
package src.corejava.v1ch14.blockingQueue;import java.io.*;import java.util.*;import java.util.concurrent.*;/** * @version 1.02 2015-06-21 * @author Cay Horstmann */public class BlockingQueueTest{ private static final int FILE_QUEUE_SIZE = 10; private static final int SEARCH_THREADS = 100; private static final File DUMMY = new File(""); private static BlockingQueue<File> queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE); public static void main(String[] args) { try (Scanner in = new Scanner(System.in)) { System.out.print("Enter base directory (e.g. /opt/jdk1.8.0/src): "); String directory = in.nextLine(); System.out.print("Enter keyword (e.g. volatile): "); String keyword = in.nextLine(); Runnable enumerator = () -> { try { enumerate(new File(directory)); queue.put(DUMMY); //因为队列是先进先出所以这里可以做一个标识如果最后消费者取到的是这个标识,那么认为已经消费完毕,程序结束 } catch (InterruptedException e) { } }; new Thread(enumerator).start(); for (int i = 1; i <= SEARCH_THREADS; i++) { Runnable searcher = () -> { try { boolean done = false; while (!done) { File file = queue.take(); if (file == DUMMY) { queue.put(file); done = true; } else search(file, keyword); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { } }; new Thread(searcher).start(); } } } /** * Recursively enumerates all files in a given directory and its subdirectories. * @param directory the directory in which to start */ public static void enumerate(File directory) throws InterruptedException { File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) enumerate(file); else queue.put(file); } } /** * Searches a file for a given keyword and prints all matching lines. * @param file the file to search * @param keyword the keyword to search for */ public static void search(File file, String keyword) throws IOException { try (Scanner in = new Scanner(file, "UTF-8"))//这种写法会自动关闭流 { int lineNumber = 0; while (in.hasNextLine()) { lineNumber++; String line = in.nextLine(); if (line.contains(keyword)) System.out.printf("%s:%d:%s%n", file.getPath(), lineNumber, line); } } }}
阅读全文
0 0
- 阻塞队列(BlockingQueue)
- 阻塞队列BlockingQueue
- 阻塞队列 BlockingQueue
- BlockingQueue: 阻塞队列 接口
- 阻塞队列BlockingQueue
- 阻塞队列(BlockingQueue)
- 阻塞队列BlockingQueue使用方法
- 阻塞队列 BlockingQueue LinkedBlockingQueue
- Java 阻塞队列 BlockingQueue
- java 阻塞队列BlockingQueue
- 阻塞队列(BlockingQueue)
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue
- 阻塞队列BlockingQueue用法
- Java : BlockingQueue 阻塞队列
- leetcode--Path Sum
- 输入并逐行输出
- android相关错误
- 浏览器内核和性能优化总结
- spring和springmvc的区别
- 阻塞队列BlockingQueue
- HDU 1008 (Elevator)
- 哪个项目管理工具好用到哭?JIRA VS 华为软件开发云
- poj3176
- 第1章:阿里云机器学习实践之路 / 第3节:推荐系统--基于协同过滤的商品推荐
- C++ Builder中实现拖放功能
- ajax提交文件到服务器
- 私有继承与保护继承
- 使用一个for循环打印二维数组和三维数组