在Java 7中使用nio.2,当您创建这样的手表服务时:
WatchService watcher = FileSystems.getDefault().newWatchService();然后,启动一个后台线程,在无限循环中轮询文件系统事件。这个线程的名称是“thread”,这在调查线程转储或分析会话时有点麻烦。
我们能更改那个线程的名称吗?
发布于 2013-08-08 15:50:02
从实现的角度来看,这似乎是不可能的。如果您不介意进行小黑客攻击,您可以找到线程并重命名它。
类似于(//TODO:设置错误检查):
Set<Thread> threadsBefore = Thread.getAllStackTraces().keySet();
WatchService ws = FileSystems.getDefault().newWatchService();
//I don't need to wait here on my machine but YMMV
Set<Thread> threadsAfter = Thread.getAllStackTraces().keySet();
threadsAfter.removeAll(threadsBefore);
Thread wsThread = threadsAfter.toArray(new Thread[1])[0];
System.out.println("wsThread = " + wsThread);
wsThread.setName("WatchService Thread");
Set<Thread> justChecking = Thread.getAllStackTraces().keySet();
System.out.println("justChecking = " + justChecking);https://stackoverflow.com/questions/18130202
复制相似问题