需要了解BlockingQueue接口,封装了Lock和Condition,使程序专注于业务逻辑
生产者只关注于爬取需要的文件,消费者只关注于给爬取到的文件建立索引,分工明确。
class FileCrawler implements Runnable {
private BlockingQueue<File> fileQueue;
private File root;
private FileFilter filter;
public FileCrawler(BlockingQueue<File> queue, File r, FileFilter f) {
this.fileQueue = queue;
this.root = r;
this.filter = f;
}
public void run() {
try {
while (!stopCraw()) {
craw(root);
}
} catch (Exception e) {
}
}
private boolean stopCraw() {
return true;
}
private void craw(File file) {
File[] subFiles = file.listFiles(filter);
if (subFiles != null) {
for(File subFile : subFiles) {
if (subFile.isDirectory()) {
craw(subFile);
} else if (!alreadyIndexed(subFile)) {
this.fileQueue.add(subFile);// 封装了 lock 和 condition
}
}
}
}
private boolean alreadyIndexed(File file) {
return false;
}
}
class Indexer implements Runnable {
private BlockingQueue<File> fileQueue;
public Indexer(BlockingQueue<File> queue) {
this.fileQueue = queue;
}
public void run() {
try {
while (true) {
indexedFile(fileQueue.take());
}
} catch(InterruptedException e) {
// 恢复被中断的状态
Thread.currentThread().interrupt();
}
}
private void indexedFile(File file) {
}
}