Skip to content

Commit

Permalink
1、优化文件搜索线程处理
Browse files Browse the repository at this point in the history
  • Loading branch information
xwintop committed Aug 5, 2019
1 parent 1911428 commit 64cf288
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xwintop.xJavaFxTool.services.littleTools;

import cn.hutool.core.thread.ThreadUtil;
import com.xwintop.xJavaFxTool.controller.littleTools.FileSearchToolController;
import com.xwintop.xJavaFxTool.utils.ConfigureUtil;
import com.xwintop.xcore.util.javafx.TooltipUtil;
Expand All @@ -16,6 +15,7 @@
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.NoLockFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.File;
import java.io.IOException;
Expand All @@ -24,6 +24,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;

/**
* @ClassName: FileSearchToolService
Expand All @@ -38,6 +39,7 @@
public class FileSearchToolService {
private FileSearchToolController fileSearchToolController;
private static Timer autoRefreshIndexTimer = null;
private static ThreadPoolTaskExecutor refreshIndexPoolTaskExecutor;
private static final String searchIndexDir = ConfigureUtil.getConfigurePath("searchIndexDir/");

private static Directory directory;
Expand All @@ -61,6 +63,13 @@ public class FileSearchToolService {
} catch (Exception e) {
log.error("创建文件失败!", e);
}
refreshIndexPoolTaskExecutor = new ThreadPoolTaskExecutor();
refreshIndexPoolTaskExecutor.setCorePoolSize(5);
refreshIndexPoolTaskExecutor.setMaxPoolSize(20);
refreshIndexPoolTaskExecutor.setQueueCapacity(30);
// 设置拒绝策略
refreshIndexPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
refreshIndexPoolTaskExecutor.initialize();
}

public static void getIndexWriter(Directory directory) throws IOException {
Expand Down Expand Up @@ -185,7 +194,7 @@ public void refreshIndexAction() throws Exception {
}

public void addSearchIndexFile(Path path) {
ThreadUtil.execute(() -> {
refreshIndexPoolTaskExecutor.execute(() -> {
try {
DirectoryStream<Path> stream = Files.newDirectoryStream(path);
Iterator<Path> pathIterator = stream.iterator();
Expand Down

0 comments on commit 64cf288

Please sign in to comment.