阻塞队列:搜索目录下所有文件中所含关键字(JAVA核心技术14-9)

普通查找

package com.yzq.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FindKey {
  public static void Find(String filefolder, String key) throws FileNotFoundException {
    // 遍历
    File in = new File(filefolder);
    Findfile(in, key);
  }

  public static void Findfile(File file, String key) throws FileNotFoundException {
    if (file.isFile()) {
      Scanner sc = new Scanner(file, "UTF-8");
      int lineNumber = 0;
      while (sc.hasNextLine()) {
        lineNumber++;
        String line = sc.nextLine();
        if (line.contains(key))
          System.out.printf("%s:%d:%s\n", file.getPath(), lineNumber, line.trim());
      }
    } else {
      if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File file1 : files) Findfile(file1, key);
      }
    }
  }

  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter base directory (e.g /opt/jdk1.8,0/src): ");
    String directory = in.nextLine();
    System.out.println("Enter keyword(e.g. volatite): ");
    String keyword = in.nextLine();
    Find(directory, keyword);
  }
}

在这里插入图片描述
阻塞队列

package com.yzq.threadex;

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

public class BlockingQueueTest {
  private static final int FILE_QUEUE_SIZE = 10;
  private static final int SEARCH_THREADS = 100;
  private static final File DUMMY = new File("");
  private static BlockingQueue<File> queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE);

  public static void main(String[] args) {
    try (Scanner in = new Scanner(System.in)) {
      System.out.println("Enter base directory (e.g /opt/jdk1.8,0/src): ");
      String directory = in.nextLine();
      System.out.println("Enter keyword(e.g. volatite): ");
      String keyword = in.nextLine();

      Runnable enumerator =
          () -> {
            try {
              enumerate(new File(directory));
              queue.put(DUMMY);
            } catch (InterruptedException e) {
            }
          };

      new Thread(enumerator).start();
      for (int i = 0; i <= SEARCH_THREADS; i++) {
        Runnable searcher =
            () -> {
              try {
                boolean done = false;
                while (!done) {
                  File file = queue.take();
                  if (file == DUMMY) {
                    queue.put(file);
                    done = true;
                  } else search(file, keyword);
                }
              } catch (IOException e) {
                e.printStackTrace();
              } catch (InterruptedException e) {
              }
            };
        new Thread(searcher).start();
      }
    }
  }

  public static void enumerate(File directory) throws InterruptedException {
    File[] files = directory.listFiles();
    for (File file : files) {
      if (file.isDirectory()) enumerate(file);
      else queue.put(file);
    }
  }

  public static void search(File file, String keyword) throws IOException {
    try (Scanner in = new Scanner(file, "UTF-8")) {
      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.trim());
      }
    }
  }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值