thunkware / virtual-threads-bridge

Use Java21's Virtual Threads API in Java 8+
MIT License
6 stars 3 forks source link

improve ThreadPerTaskExecutor clean up #9

Closed maxxedev closed 8 months ago

maxxedev commented 8 months ago

In rare cases, ThreadPerTaskExecutor may cause memory leak. Improve clean up of threads

Make a change so that the code looks like this, before: https://github.com/thunkware/virtual-threads-backport/blob/f4b41439f45e7afa988c2b2aede2dfd71636a2f0/src/main/java/com/github/thunkware/ThreadProvider8.java#L78-L81

and after:

             Thread thread = threadFactory.newThread(() -> {
+                try {
+                    command.run();
+                } finally {
+                    threads.remove(Thread.currentThread());
+                }
             });