Executors.newFixedThreadPool hanged topics

As said in javadoc

Threads in the pool will exist until it is explicitly disabled ExecutorService#shutdown()

If I have a web application on Tomcat. At startup, creates a fixed pool of threads. I also researched

  public static void main(String ... strings) { ExecutorService s = Executors.newFixedThreadPool(2); s.submit(new Runnable() { public void run() { System.out.println("zzz"); } }); } 

that the threads in the above example do not exist until I send them to the ExecutorService. When the main method ends, I see a javaw.exe in the process list of the manger task (win 7 os). Therefore, I assume that the jvm instance that runs this example still exists. When I add s.shutdown() - there is no java process in the process list.

Question 1 : when tomcat suddenly stops due to some errors, will the java process freeze in memory (if previously some tasks were sent to the thread pool mentioned above)

Question 2 : if the answer to the previous question is yes, are there ways to make threads in the pool a deban, or maybe there are some ways to handle such tomcat / myapp stops to call ExecutorService#shutdown()

+4
source share
1 answer

Question 1: when tomcat suddenly stops due to some errors, will the java process freeze in memory (if previously some tasks were sent to the thread pool mentioned above)

If it does not disable ExecutorService , then yes, it will hang.

Question 2: if the answer to the previous question is yes, are there ways to make threads in the pool deban ...

Yes there is. You can provide a factory stream.

 ExecutorService threadPool = newFixedThreadPool(numThreads, new ThreadFactory() { public Thread newThread(Runnable runnable) { Thread thread = Executors.defaultThreadFactory().newThread(runnable); thread.setDaemon(true); return thread; } }); 

Edit:

As mentioned in @matt b, another mechanism is to define a hook / shutdown listener in Tomcat:

Disconnect key for java web application

+10
source

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


All Articles