gpt4 book ai didi

Java 线程处理与 walkFileTree

转载 作者:行者123 更新时间:2023-12-01 09:01:45 25 4
gpt4 key购买 nike

我是java线程过程的新手,我找到了一个示例代码,我有兴趣了解它们的代码如何流动,所以我可以将其放入我的代码中。这是我的reference 。我将使用 Files.walkFileTree() 向 WatchService 注册主目录和子目录中所有新创建的文件夹

这是我的示例文件夹结构
< br/>

Root : C:/REST API/source/
sub directory : /source/abc/



首先,我将向 WatchService 注册我的根目录,并创建文件夹 abc。但是,当我尝试像之前描述的那样进行测试时,我无法完全理解编译调试消息的流程

这是我的编译器消息

Register path for Watcher Service
The Main Path is :C:\REST API\source
The sub path is :C:\REST API\source
In RegisterDIR method () path valueC:\REST API\source
Register path for Watcher Service
The Main Path is :C:\REST API\source
The sub path is :C:\REST API\source
In RegisterDIR method () path valueC:\REST API\source
The Main Path is :C:\REST API\source
The sub path is :C:\REST API\source\New folder
In RegisterDIR method () path valueC:\REST API\source\New folder
Register path for Watcher Service
The Main Path is :C:\REST API\source
The sub path is :C:\REST API\source
In RegisterDIR method () path valueC:\REST API\source
The Main Path is :C:\REST API\source
The sub path is :C:\REST API\source\abc
In RegisterDIR method () path valueC:\REST API\source\abc



这是我的示例代码

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class AbsolutePath<WatchListener>
{
static Path mainPath;
static WatchService svc;
Thread watch;
static Map <WatchKey , Path> keyMap;
WatchKey wk;
Timer timer;
WatchListener lin;


public static void main(String [] args ) throws IOException
{

mainPath = Paths.get("C:/REST API/source/");

svc = FileSystems.getDefault().newWatchService();

keyMap = new HashMap<>();

AbsolutePath ab = new AbsolutePath();

ab.start();
}

private void start() throws IOException
{

watch = new Thread(new Runnable()
{
public void run()
{
if(!Thread.currentThread().isInterrupted())
{

walkTreeAndSetWatches();
while(true)
{
try
{

wk = svc.take();

wk.pollEvents();

wk.reset();

newRegister();

}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

});

watch.start();
}
private synchronized void walkTreeAndSetWatches()
{
System.out.println("Register path for Watcher Service");
try
{
Files.walkFileTree(mainPath, new FileVisitor<Path>()
{
@Override
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attr) throws IOException
{
System.out.println("The Main Path is :"+mainPath);
System.out.println("The sub path is :"+path);
if(attr.isDirectory())
{

registerDIR(path);
}
return FileVisitResult.CONTINUE;
}



@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
{

return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{

return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException
{

return FileVisitResult.CONTINUE;
}


});
}
catch(IOException io)
{
io.printStackTrace();
}
}

private void registerDIR(Path path)
{
try
{
System.out.println("In RegisterDIR method () path value"+path);
keyMap.put(path.register(svc, StandardWatchEventKinds.ENTRY_CREATE),path);

}

catch (IOException e)
{
e.printStackTrace();
}

}

private synchronized void newRegister()
{
timer = new Timer();

timer.schedule(new TimerTask()
{

@Override
public void run()
{

walkTreeAndSetWatches();


}


},3000);
}

}



有人可以帮助我理解为什么我的代码流变得这样,是因为线程句柄有问题吗?在我的引用链接中,如果在受监视的目录中创建了文件夹/文件,他们如何处理事件,因为我找不到它如何处理此类事件

最佳答案

发生这种情况是因为在 while 循环中您调用了 newRegister() ,而 newRegister() 又从静态的 mainPath 开始。一些建议:

  • 重构代码以消除静态,
  • 这是一个递归算法,因此在注册新路径时需要将新路径传递给新的walker。

附加问题:您不喜欢原始代码中的哪些内容而导致您重写了它? :)(另一种说法:您想要添加/删除哪种功能?)

关于Java 线程处理与 walkFileTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41612080/

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