blockingQueue毒丸对象使用
来源:互联网 发布:java 开源工作流 编辑:程序博客网 时间:2024/06/11 15:43
package com.mylearn.threadpool.blockingqueue;
import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by IntelliJ IDEA.
* User: yingkh
* Date: 13-8-1
* Time: 上午7:34
* To change this template use File | Settings | File Templates.
*/
public class BlockingQueueTest {
private static AtomicInteger count = new AtomicInteger(0);
private static AtomicInteger countCreate = new AtomicInteger(0);
static BlockingQueue fileQueue = new ArrayBlockingQueue(5);
private static File dummy =new File(""); //毒丸对象
public static void main(String args[]) {
String path = "F:\\Song";
File root = new File(path);
new Thread(new FileCrawler( root)).start();
CountDownLatch countDownLatch =new CountDownLatch(7);
for (int i = 0; i < 7; i++) {
new Thread(new Indexer(countDownLatch)).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println("生产者生产:" + countCreate.get());
System.out.println("消费者取到:" + count.get());
}
static class FileCrawler implements Runnable {
private final File root;
public FileCrawler(File file) {
this.root = file;
}
public void run() {
try {
System.out.println("生产者开始生产:" + fileQueue.size());
crawl(root);
fileQueue.put(dummy); //注意这里,放入一个毒丸
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void crawl(File root) throws InterruptedException {
File[] files = root.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
crawl(file);
} else {
fileQueue.put(file);
countCreate.incrementAndGet();
}
}
}
}
}
static class Indexer implements Runnable {
private CountDownLatch countDownLatch;
Indexer(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public void run() {
boolean flag=true ; //flag,作为循环退出的依据
while (flag) {
try {
System.out.println("消费者开始消费:" + fileQueue.size());
File file = (File) fileQueue.take();
if(file ==dummy){ //如果取出的是毒丸对象,先重新放进队列,然后改变flag的值,退出此线程
fileQueue.put(dummy);
flag=false;
} else {
count.incrementAndGet();
System.out.println(file.getName());
}
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
System.out.println(Thread.currentThread().getName()+ "线程结束");
countDownLatch.countDown();
}
}
}
import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by IntelliJ IDEA.
* User: yingkh
* Date: 13-8-1
* Time: 上午7:34
* To change this template use File | Settings | File Templates.
*/
public class BlockingQueueTest {
private static AtomicInteger count = new AtomicInteger(0);
private static AtomicInteger countCreate = new AtomicInteger(0);
static BlockingQueue fileQueue = new ArrayBlockingQueue(5);
private static File dummy =new File(""); //毒丸对象
public static void main(String args[]) {
String path = "F:\\Song";
File root = new File(path);
new Thread(new FileCrawler( root)).start();
CountDownLatch countDownLatch =new CountDownLatch(7);
for (int i = 0; i < 7; i++) {
new Thread(new Indexer(countDownLatch)).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println("生产者生产:" + countCreate.get());
System.out.println("消费者取到:" + count.get());
}
static class FileCrawler implements Runnable {
private final File root;
public FileCrawler(File file) {
this.root = file;
}
public void run() {
try {
System.out.println("生产者开始生产:" + fileQueue.size());
crawl(root);
fileQueue.put(dummy); //注意这里,放入一个毒丸
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void crawl(File root) throws InterruptedException {
File[] files = root.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
crawl(file);
} else {
fileQueue.put(file);
countCreate.incrementAndGet();
}
}
}
}
}
static class Indexer implements Runnable {
private CountDownLatch countDownLatch;
Indexer(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public void run() {
boolean flag=true ; //flag,作为循环退出的依据
while (flag) {
try {
System.out.println("消费者开始消费:" + fileQueue.size());
File file = (File) fileQueue.take();
if(file ==dummy){ //如果取出的是毒丸对象,先重新放进队列,然后改变flag的值,退出此线程
fileQueue.put(dummy);
flag=false;
} else {
count.incrementAndGet();
System.out.println(file.getName());
}
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
System.out.println(Thread.currentThread().getName()+ "线程结束");
countDownLatch.countDown();
}
}
}
0 0
- blockingQueue毒丸对象使用
- BlockingQueue使用
- BlockingQueue 使用
- BlockingQueue 使用
- 新浪的毒丸....
- 什么是毒丸计划
- 线程BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue简介及使用
- BlockingQueue的使用
- BlockingQueue的使用
- BlockingQueue的使用
- WPF多线程
- libev源代码分析--事件模型 ae
- Ubuntu GCC不能编译sqrt()函数问题
- strcpy/strncpy/strcpy_s比较
- iOS 应用内支付(IAP)
- blockingQueue毒丸对象使用
- Leetcode: Valid Sudoku
- libev源代码分析--总体框架
- 黑马程序员-->07_面向对象
- 在xp或win7下 oracle11g通过exp/imp方式来对db数据的备份与还原
- C#构造函数
- 深入理解容器系列片之二--------ArrayList的总结
- 初次实验mysql游标
- 利用中位数进行快速排序