Why is Thread Executer itself still in memory after shutdown?

Using Java and App Server Deploy the application in which the thread runs.

During deployment, I will ask executer to shut down. This successfully cancels all tasks. However, through VisualVM, I still see the stream that the executor represents, and it is in a waiting state. I do not support any execter references since the entire application is not being deployed. Therefore, if I repeat the deployment-deployment cycle several times, I see how the number of threads grows.

How can I get rid of them?

UPDATE:

code example

here is the code:

public void startScheduler()
{
    if (scheduledExecutor == null)
    {
        scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("My ScheduledExecutor"));
        processFuture = scheduledExecutor.scheduleAtFixedRate(new Runnable()
            {
                @Override
                public void run()
                {
                    startProcessor();
                }
            }, 0, 84600, TimeUnit.SECONDS);
    }
}


public void stopScheduler()
{

    if (processFuture != null)
    {
        processFuture.cancel(true);
        processFuture = null;
    }

    if (scheduledExecutor != null)
    {
        try
        {
            scheduledExecutor.shutdownNow();
            scheduledExecutor.awaitTermination(10, TimeUnit.SECONDS);
        }
        catch (InterruptedException ignored)
        {}
        finally
        {
            scheduledExecutor = null;
        }
    }

}
+3
source share
2 answers

, ", ". /id/threadgroup? , - .

( ThreadFactory). A Thread , Thread.currentThread(). - deploy/undeploy Thread ContextClassLoader, . ContextClassLoader -, ClassLoader. , , , ThreadLocals WebappClassLoader, ClassLoader.

+2

, shutdown, , . javadoc Executors.newSingleThreadExecutor, .

javadoc shutdownNow :

, , . , Thread.interrupt(), , - , .

( InterruptedExceptions ), , . - , , JVM. .

+1

Source: https://habr.com/ru/post/1786967/


All Articles