多线程扫描目录

</pre><pre name="code" class="java">package com.rdt.main;
import java.io.File;
import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

import com.rdt.thread.Producator;


public class Test_15 {
	
	static Logger logger = Logger.getLogger(Producator.class);
	static ArrayList<String> urlList = new ArrayList<String>();
	public static final int THREAD_POOL_SIZE = 20;          
	private final File POISON = new File("");
	private final BlockingQueue<File> files = new LinkedBlockingQueue<File>(40000);
	private final ConcurrentLinkedQueue<String> infos = new ConcurrentLinkedQueue<String>();
	private Thread[] pool = null;
	private volatile boolean running = false;
	
	public Test_15() {
		this(THREAD_POOL_SIZE);
	}
	
	public Test_15(int poolSize) {
		pool = new Thread[poolSize];
		FileWorker worker = new FileWorker();
		for(int i=0;i<pool.length;i++){
			pool[i] = new Thread(worker,"线程"+(i+1));
			pool[i].start();
		}
		running = true;
	}
	
	private class FileWorker implements Runnable {
		public void run() {
			File file = null;
			try {
				while((file=files.take()) != POISON){
					try {
						doWork(file);
//						ReadDisk.listAll(new File("E:\\Jobs\\box\\InDataPool"), 0);
					} catch (Exception e) {
						onException(e,file);
					}
				}
				files.put(POISON);
			} catch (InterruptedException e) {
			}
		}

		private void onException(Exception e, File file) {
			e.printStackTrace();
		}
		
		private void doWork(File file) {

            try {

                logger.info("当前使用的线程是:"
                        + Thread.currentThread().getName() + ",正在读文件:"
                        + file.getName() + ",列表当前长度:"
                        + files.size());
                urlList.add(file.getName());

            } catch (Exception e) {
                e.printStackTrace();
            }
		}
	}
	
	public void addFile(File file) throws InterruptedException{
		files.put(file);
	}
	
    public void addFilesIgnoreInterrupted(File dir, int level) {
        File files[]=dir.listFiles();
        if(null==files){
            return;
        }
        for(File file:files){
            if(file.isDirectory()){
            	addFilesIgnoreInterrupted(file, ++level);
            }else{
//                logger.info("读取的路径:"+file.getAbsolutePath());
                try {
                    this.files.put(file);
                } catch (InterruptedException e) {
                
                }
            }
        }
    }
	
	public void shutdown(){
		try {
			if(running){
				running = false;
				files.put(POISON);
			}
		} catch (InterruptedException e) {
		}
	}
	
	public void waiting(){
		if(running || !files.contains(POISON)){
			throw new IllegalStateException("You must call shutdown() function before.");
		}
		for(Thread t : pool){
			try {
				t.join();
			} catch (InterruptedException e) {
			}
		}
	}
	
	public Queue<String> getInfos(){
		return infos;
	}
	
	public static void main(String[] args) {
		long a = System.currentTimeMillis();
		Test_15 instance = new Test_15();
		String strurl = "E:\\jygc\\webapp\\jobs\\box\\InDataPool";
		File folder = new File(strurl);//E:\\2013-09-13   E:\\jygc\\webapp\\jobs\\box\\InDataPool\\2013-09-19
		instance.addFilesIgnoreInterrupted(folder,0);
		instance.shutdown();
		instance.waiting();
		logger.info("读取所用时间:"+(System.currentTimeMillis() - a));
		logger.info(urlList.size()+"  "+urlList);

	}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值