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();
        }
    }


}










0 0
原创粉丝点击