gpt4 book ai didi

java - 无法停止 ExecutorService

转载 作者:行者123 更新时间:2023-12-02 06:18:40 26 4
gpt4 key购买 nike

我尝试使用executorService.shutdown();结束ExecutorService,如果这不起作用,则使用executorService.shutdown();。问题是,executorService 无法停止,程序仍在运行。

这是我的类,它将启动 WatchService 来监视目录的更改:

public class FileWatcher {
/**
* This Class starts a WatchService to get changes on a specific folder.
*/

Path dir;

private final ConfigManager configManager;

private final FileHandler fileHandler;

private ExecutorService executorService;

public FileWatcher(ConfigManager configManager, FileHandler fileHandler) {
this.configManager = configManager;
this.fileHandler = fileHandler;
}

public void start() {
dir = configManager.getConfig().getJdfPath();

//executorService = Executors.newFixedThreadPool(1);
executorService = Executors.newSingleThreadExecutor();
Runnable runWatcher;
runWatcher = new Runnable() {
@Override
public void run() {
try {
startWatcher();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

executorService.execute(runWatcher);
}

private void startWatcher() throws IOException, InterruptedException {
/**
* Create a new WatchService which detects created and modified files. To
* let it detect deleted files add ENTRY_DELETE to dir.register().
*/

WatchService watcher = FileSystems.getDefault().newWatchService();
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);

while (!Thread.currentThread().isInterrupted()) {
key = waitForEvents(watcher, key);

}
}

private WatchKey waitForEvents(WatchService watcher, WatchKey key) {

/**
* The WatchService tells the FileHandler the Filename of the changed or
* new file in the given folder.
*/

try {
key = watcher.take();
} catch (InterruptedException e) {
executorService.shutdown();

} catch (ClosedWatchServiceException e) {
executorService.shutdown();

}

for (WatchEvent<?> event : key.pollEvents()) {

fileHandler.setPath((Path) event.context());
}
key.reset();
return key;
}

public void stop() {
stopWatcher();
}

private void stopWatcher() {
executorService.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
executorService.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!executorService.awaitTermination(10, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
executorService.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}

我从 oracle java api 获取了方法 stopWatcher()] 1 。我编写了一个简单的类来测试 FileWatcher 类。

public class TestFileWatcher {
static Path path;
static ConfigManager configmanager;

public static void main(String[] args) {

path = Paths.get("D:/testfolder");
FileHandler filehandler = new FileHandler() {

@Override
public void setPath(Path path) {
System.out.println("file: " + path);

}

@Override
public Path getPath() {
// TODO Auto-generated method stub
return null;
}
};

final Config config;
config = new Config() {

@Override
public Path getJdfPath() {
// TODO Auto-generated method stub
return path;
}
};

configmanager = new ConfigManager() {

@Override
public void injectConfig(Config config) {
// TODO Auto-generated method stub

}

@Override
public Config getConfig() {
// TODO Auto-generated method stub
return config;
}
};



configmanager.injectConfig(config);
filehandler.setPath(path);
final FileWatcher filewatcher = new FileWatcher(configmanager, filehandler);
filewatcher.start();
Timer timer = new Timer();


boolean stopped;

TimerTask closeWatcherTask = new TimerTask(){
@Override
public void run() {

System.out.println("filewatcher stopped.");
filewatcher.stop();

}
};
long duration = 2000;
timer.schedule(closeWatcherTask, duration);
return;

}

}

因此,当我启动测试应用程序来启动 FileWatcher 并在 2 秒后结束它时,程序告诉我:池没有终止。我可以更改什么才能正确终止它?

顺便说一句,ConfigManager 和 FileHandler 是接口(interface),我可以在其中获取必须进行更改的路径。

最佳答案

如果不分析您的代码,它不会关闭,因为任何线程尚未完成/正在占用资源。简单的肮脏解决方案是使用 javadoc 中声明的 ExcuterService.shutdownNow() 强制停止:

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

因此,请尝试此操作并分析由 ExecuterService.shutdownNow() 返回的 Runnables 列表,您可以返回查看哪个线程正在挂起,然后进行逐步调试并查看导致挂起的原因.

关于java - 无法停止 ExecutorService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21216892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com