ArrayBlockingQueue调度多线程demo

package blockingqueue;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;

public class SearchTask implements Runnable{
  private BlockingQueue<File> queue;
  private String keyword;
 
  public SearchTask(BlockingQueue<File> queue,String keyword){
    this.queue = queue;
    this.keyword = keyword;
  }
 
  public void search(File file) throws IOException {
    Scanner in = new Scanner(new FileInputStream(file));
    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);
      }
    }
    in.close();
  }
 
  @Override
  public void run() {
    try{
      boolean done = false;
      while(!done){
        File file = queue.take();
        if(file == FileEnumerationTask.DUMY){
          queue.put(file);
          done = true;
        }else{
          search(file);
        }
      }
    }catch(Exception e){
     
    }
  }

}

 

package blockingqueue;

import java.io.File;

public class FileEnumerationTask implements Runnable{

  public static File DUMY = new File("");
  private BlockingQueue<File> queue;
  private File startingDirectory;
 
  public FileEnumerationTask(BlockingQueue<File> queue,File startingDirectory){
    this.queue = queue;
    this.startingDirectory = startingDirectory;
  }
 
  public void enumerate(File directory) throws InterruptedException{
    File[] files = directory.listFiles();
    for(File file:files){
      if(file.isDirectory()){
        enumerate(file);
      }else{
        queue.put(file);
      }
    }
  }
 
  @Override
  public void run() {
    try{
      enumerate(startingDirectory);
      queue.put(DUMY);
    }catch(Exception e){
     
    }
  }

}

 

package blockingqueue;

import java.io.File;

public class BlockingQueueTest {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter base directory(e:/nice)");
    String directory = in.nextLine();
    System.out.println("Enter keyword(good)");
    String keyword = in.nextLine();
    final int FILE_QUEUE_SIZE = 10;
    final int SEARCH_THREADS = 100;
    BlockingQueue<File> queue = new ArrayBlockingQueue<File>(FILE_QUEUE_SIZE);
    FileEnumerationTask enumerator = new FileEnumerationTask(queue,new File(directory));
    new Thread(enumerator).start();
    for(int i = 1; i<= SEARCH_THREADS;i++){
      new Thread(new SearchTask(queue,keyword)).start();
    }
  }
}

 

你可能感兴趣的:(ArrayBlockingQueue调度多线程demo)