ThreadLocal are closed after the thread using them dies. If you want control over this you need to use a map instead.
// do our own thread local resources which close when we want.
Map<Thread, Resource> threadLocalMap = new ConcurrentHashMap<>();
fj.submit(
() -> myStream.parallel().forEach(e -> {
Resource r = threadLocalMap.computeIfAbsent(Thread.currentThread(), t -> new Resource();
// use the thread local autocloseable here,
})
// later once all the tasks have finished.
// close all the thread local resources when the parallel processing is done
threadLocalMap.values().forEach(Utils::closeQuietly);
It's common to have a method which closes resources without throwing an exception. Chronicle has one but so do many other libraries.
public static void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ioe) {
// ignore or trace log it
}
}
}
Most likely you have a method do this in your project already
https://www.google.co.uk/search?q=public+static+void+closequietly+Closeable