程序清单

 

 

程序清单 5-8 桌面搜索应用程序中的生产者任务和消费者任务(和书上有所不同,有些许改动)


 

1、生产者

package com.everjiankang.miaosha;

import java.io.File;
import java.io.FileFilter;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * 文件爬虫程序
 * @author guchunchao
 *
 */
public class FileCrawer implements Runnable {

    private final BlockingQueue fileQueue;
    private final FileFilter fileFilter;
    private final File root;
    private final Set alreadyExistFileSet = new ConcurrentSkipListSet<>();
        
    public FileCrawer(BlockingQueue fileQueue, FileFilter fileFilter, File root) {
        super();
        this.fileQueue = fileQueue;
        this.fileFilter = fileFilter;
        this.root = root;
    }


    @Override
    public void run() {
        try {
            this.crawl(root);
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

    }
    
    
    private void crawl(File root) throws InterruptedException {
        if(root != null) {
            File[] entries = root.listFiles(fileFilter);
            if(entries != null) {
                for(File file : entries) {
                    if(file.isDirectory()) 
                        crawl(file);
                    else if(!alreadyIndexed(file)) {
                        System.out.println("生产者放入队列:" + file.getPath() + file.getName());
                        fileQueue.put(file);
                        alreadyExistFileSet.add(file.getPath()+file.getName());
                    }
                }
            } 
        }
    }
    
    private boolean alreadyIndexed(File file) {
        return alreadyExistFileSet.contains(file.getPath()+file.getName());
//        return fileQueue.contains(file);
    }

}

 

2、消费者

package com.everjiankang.miaosha;

import java.io.File;
import java.util.concurrent.BlockingQueue;

/**
 * 消费者,建立文件索引
 * @author guchunchao
 *
 */
public class Indexer implements Runnable {
    
    private final BlockingQueue queue;
    
    public Indexer(BlockingQueue queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            while(true) {
                indexFile(queue.take());
            }
        } catch(InterruptedException e) {
            Thread.currentThread().interrupt();
        }

    }

    private void indexFile(File file) {
        System.out.println("消费:" + file.getPath()+file.getName());
    }

}

 

3调用

package com.everjiankang.miaosha;

import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class XiaoFeiQueue {
    private final static int BOUND = 10;
    private final static int N_CONSUMMERS = 10;

    public static void main(String[] args) {
        File file = new File("/Users/guchunchao/Desktop/深入理解java虚拟机视频1-6");
        File[] files = new File[10];
        files[0] = file;
        startIndexing(files);

    }
    
    public static void startIndexing(File[] files) {
        BlockingQueue queue = new LinkedBlockingDeque(BOUND);
        FileFilter filter = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return true;
            }
        };
        
        //n个线程爬取文件
        for(File file : files) {
            new Thread(new FileCrawer(queue, filter, file)).start();
        }
        
        //10个线程消费
        for(int i = 0; i < N_CONSUMMERS; i++) {
            new Thread(new Indexer(queue)).start();
        }
    }

}

 


 

 

 

程序清单 5-11 在计测试中使用CountDownLatch来启动和停止线程


 

 

public static void say(int nThread, Runnable run) throws InterruptedException {
        CountDownLatch startDoor =new CountDownLatch(1);
        CountDownLatch endDoor = new CountDownLatch(nThread);
        for(int i = 0; i < nThread; i++) {
            new Thread() {
                @Override
                public void run() {
                    try {
                        startDoor.await();
                        try {
                            run.run();
                        }finally {
                            endDoor.countDown();
                        }
                    } catch (InterruptedException e) {
                    }
                }
            }.start();
        }
        
        long startTime = System.nanoTime();
        startDoor.countDown();
        endDoor.await();
        long endTime = System.nanoTime();
        System.out.println(nThread + "个线程总共花费了" + (endTime - startTime) + " ns");
}

 

转载于:https://www.cnblogs.com/guchunchao/p/10658372.html

你可能感兴趣的:(程序清单)