Using nio.2 in Java 7, when you create a watch service like that:
WatchService watcher = FileSystems.getDefault().newWatchService();
Then, a background thread is started, polling for file system events within an infinite loop. The name of this thread is "Thread-n" which is a bit of a nuisance when investigating thread dumps or during profiling sessions.
Can we change the name of that thread?
Looking at the implementation, it does not seem possible directly. If you don't mind a little hack, you can find the thread and rename it.
Something like (//TODO: put error checks in place):
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With