Graceful shutdown of threads and executor

前端 未结 5 1505
一个人的身影
一个人的身影 2020-11-29 18:42

The following piece of code tries to accompolish this.

The code loops forever and checks if there are any pending requests to be processed. If there is any, it creat

5条回答
  •  情深已故
    2020-11-29 19:25

    I had similar issue, i use to get error like

    o.a.c.loader.WebappClassLoaderBase :: The web application [ROOT] appears to have started a thread named [pool-2-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)

    Bellow code fixed it

    private ThreadPoolExecutor executorPool;
    
    @PostConstruct
    public void init() {
        log.debug("Initializing ThreadPoolExecutor");
        executorPool = new ThreadPoolExecutor(1, 3, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(1));
    }
    
    @PreDestroy
    public void destroy() {
        log.debug("Shuting down ThreadPoolExecutor");
        executorPool.shutdown();
    }
    

提交回复
热议问题