gpt4 book ai didi

java - 如何杀死 watch 服务线程

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:10 28 4
gpt4 key购买 nike

我需要根据来 self 的 UI 的请求观看 HotFolder我的用户界面上有开始和停止按钮,当我单击开始时,我的代码应该监视该文件夹,单击停止时,它应该停止监视它。我正在使用监视服务来监视 HotFolder,我将标志从 Controller 传递给监视服务以启动和停止监视文件夹。请建议我如何停止观看该文件夹?

下面是代码片段:

@RequestMapping(value = "/start", method = RequestMethod.GET)
public ModelAndView hotFolder()
{
ModelAndView model = new ModelAndView();
model.setViewName("welcomePage");

HotFolder h = new HotFolder();
h.hotfolderTesting(true);
return model;
}

@RequestMapping(value = "/stop", method = RequestMethod.POST)
public ModelAndView hotFolderStop()
{
ModelAndView model = new ModelAndView();
model.setViewName("welcomePage");

HotFolder h = new HotFolder();
h.hotfolderTesting(false);
return model;
}

HotFolder.java:

public void hotfolderTesting(boolean flag)
{
try (WatchService service = FileSystems.getDefault().newWatchService()) {
Map<WatchKey, Path> keyMap = new HashMap<>();
Path path = Paths.get("E:\\TestingWatch");
keyMap.put(path.register(service, StandardWatchEventKinds.ENTRY_CREATE), path);

WatchKey watchKey;
if (flag) {
while (true) {
watchKey = service.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
System.out.println("Created: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
System.out.println("Deleted: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
System.out.println("Modified :" + event.context());
}
}
if (!watchKey.reset()) {
break;
}
}
}
} catch (Exception ignored) {
}
}

最佳答案

肯定有一些你没有提到的事情,因为我看到的注释让我认为你正在使用某种基于 REST 的库。

无论如何,你的代码永远不会工作。那是因为你基本上将回答你的问题的线程变成了观看线程,而这永远不会完成你的工作。我建议使用 Singleto 模式,像这样编写 HotFolder 类:

public class HotFolder implements Runnable{
private static HotFolder instance = null;

public static HotFolder getInstance(){
if(instance == null)
instance = new HotFolder();
return instance;
}

private boolean running = false;
private Thread t = null
private HotFolder(){
}

public setRunning(boolean running){
this.running = running;
if(running && t == null){
t = new Thread(this);
t.start()
}else if(!running && t!= null){
t = null;
}
}

public boolean getRunning(){
return running;
}

public void run(){
try (WatchService service = FileSystems.getDefault().newWatchService()) {
Map<WatchKey, Path> keyMap = new HashMap<>();
Path path = Paths.get("E:\\TestingWatch");
keyMap.put(path.register(service, StandardWatchEventKinds.ENTRY_CREATE), path);
WatchKey watchKey;
watchKey = service.take();
do{
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
System.out.println("Created: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
System.out.println("Deleted: " + event.context());
} else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
System.out.println("Modified :" + event.context());
}
}
}while(running && watchKey.reset());
} catch (Exception ignored) {
}
}
}

然后你就这样调用它

//to activate
HotFolder.getInstance().setRunning(true)
//to stop it
HotFolder.getInstance().setRunning(false)

关于java - 如何杀死 watch 服务线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174018/

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