package org.helioviewer.jhv.threads;

import java.lang.ref.WeakReference;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class JHVExecutor {

    public static final ExecutorService cachedPool = createCachedPool();

    private static ExecutorService createCachedPool() {
        ExecutorService service = Executors.newCachedThreadPool(new JHVThread.NamedThreadFactory("Worker"));
        shutdownOnDisposal(service);
        return service;
    }

    private static void shutdownOnDisposal(ExecutorService es) {
        Runnable shutdownHook =
                new Runnable() {
                    final WeakReference<ExecutorService> executorServiceRef = new WeakReference<>(es);

                    @Override
                    public void run() {
                        ExecutorService executorService = executorServiceRef.get();
                        if (executorService != null) {
                            AccessController.doPrivileged(
                                    (PrivilegedAction<Void>) () -> {
                                        executorService.shutdown();
                                        return null;
                                    });
                        }
                    }
                };

        AccessController.doPrivileged(
                (PrivilegedAction<Void>) () -> {
                    Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook));
                    return null;
                });
    }

}

@@ -71,86 +71,8 @@
 ce;%0A
-import java.security.AccessController;%0Aimport java.security.PrivilegedAction;%0A
 impo
@@ -630,16 +630,24 @@
         
+private 
 final We
@@ -928,18 +928,16 @@
 != null)
- %7B
 %0A       
@@ -961,437 +961,78 @@
     
-AccessController.doPrivileged(%0A                                    (PrivilegedAction%3CVoid%3E) () -%3E %7B%0A                                        executorService.shutdown();%0A                                        return null;%0A                                    %7D);%0A                        %7D%0A                    %7D%0A                %7D;%0A%0A        AccessController.doPrivileged(%0A                (PrivilegedAction%3CVoid%3E) () -%3E %7B%0A            
+executorService.shutdown();%0A                    %7D%0A                %7D;%0A%0A
     
@@ -1103,61 +1103,8 @@
 ));%0A
-                    return null;%0A                %7D);%0A
     

Remove future deprecated