Skip to content

Commit 64cf288

Browse files
committed
1、优化文件搜索线程处理
1 parent 1911428 commit 64cf288

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/services/littleTools/FileSearchToolService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.xwintop.xJavaFxTool.services.littleTools;
22

3-
import cn.hutool.core.thread.ThreadUtil;
43
import com.xwintop.xJavaFxTool.controller.littleTools.FileSearchToolController;
54
import com.xwintop.xJavaFxTool.utils.ConfigureUtil;
65
import com.xwintop.xcore.util.javafx.TooltipUtil;
@@ -16,6 +15,7 @@
1615
import org.apache.lucene.store.Directory;
1716
import org.apache.lucene.store.FSDirectory;
1817
import org.apache.lucene.store.NoLockFactory;
18+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
1919

2020
import java.io.File;
2121
import java.io.IOException;
@@ -24,6 +24,7 @@
2424
import java.nio.file.Path;
2525
import java.nio.file.Paths;
2626
import java.util.*;
27+
import java.util.concurrent.ThreadPoolExecutor;
2728

2829
/**
2930
* @ClassName: FileSearchToolService
@@ -38,6 +39,7 @@
3839
public class FileSearchToolService {
3940
private FileSearchToolController fileSearchToolController;
4041
private static Timer autoRefreshIndexTimer = null;
42+
private static ThreadPoolTaskExecutor refreshIndexPoolTaskExecutor;
4143
private static final String searchIndexDir = ConfigureUtil.getConfigurePath("searchIndexDir/");
4244

4345
private static Directory directory;
@@ -61,6 +63,13 @@ public class FileSearchToolService {
6163
} catch (Exception e) {
6264
log.error("创建文件失败!", e);
6365
}
66+
refreshIndexPoolTaskExecutor = new ThreadPoolTaskExecutor();
67+
refreshIndexPoolTaskExecutor.setCorePoolSize(5);
68+
refreshIndexPoolTaskExecutor.setMaxPoolSize(20);
69+
refreshIndexPoolTaskExecutor.setQueueCapacity(30);
70+
// 设置拒绝策略
71+
refreshIndexPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
72+
refreshIndexPoolTaskExecutor.initialize();
6473
}
6574

6675
public static void getIndexWriter(Directory directory) throws IOException {
@@ -185,7 +194,7 @@ public void refreshIndexAction() throws Exception {
185194
}
186195

187196
public void addSearchIndexFile(Path path) {
188-
ThreadUtil.execute(() -> {
197+
refreshIndexPoolTaskExecutor.execute(() -> {
189198
try {
190199
DirectoryStream<Path> stream = Files.newDirectoryStream(path);
191200
Iterator<Path> pathIterator = stream.iterator();

0 commit comments

Comments
 (0)