首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Java 7 NIO.2)监视服务线程的自定义名称

(Java 7 NIO.2)监视服务线程的自定义名称
EN

Stack Overflow用户
提问于 2013-08-08 15:35:01
回答 1查看 481关注 0票数 6

在Java 7中使用nio.2,当您创建这样的手表服务时:

代码语言:javascript
复制
WatchService watcher = FileSystems.getDefault().newWatchService();

然后,启动一个后台线程,在无限循环中轮询文件系统事件。这个线程的名称是“thread”,这在调查线程转储或分析会话时有点麻烦。

我们能更改那个线程的名称吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-08 15:50:02

从实现的角度来看,这似乎是不可能的。如果您不介意进行小黑客攻击,您可以找到线程并重命名它。

类似于(//TODO:设置错误检查):

代码语言:javascript
复制
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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18130202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档