Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java process halts for infinite time and cannot exit properly?

i have java process that sends emails for different email address and i am using java thread pool executor, each thread try to send e-mail and then exits

the problem is all the threads are going to wait state and can never come back to running state even the threads have completed their working successfully,

my threadPoolExecutor configuration is as follows,

queue size = 100 thread count = 5 max thread count = 10 keep alive time = 1 min

here is the thread dump but i didn't understand what is says

java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x2808f538> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(Unknown Source)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
    at java.util.concurrent.ArrayBlockingQueue.take(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

   Locked ownable synchronizers:
    - None

Here is the code:

ReqtQueue = new ArrayBlockingQueue<Runnable>(100, true);
        notifTaskExecutor   = new ThreadPoolExecutor(
          5, // core size of threads that will remain in idle state
          10, // max size of thread that can be created
          1, // keep alive time for thread other than number of core threads when they are in idel state
          TimeUnit.MINUTES, // keep alive time units
          ReqtQueue // the queue to use
        );

And the excute method is called:

notifTaskExecutor.execute(new NotificationSender(request, name));

the NotificationSender just send the email and all code is in try catch and its very simple, and even when the run method is empty the thread is not terminated

help required

regards

like image 458
Badr Avatar asked Jul 02 '26 06:07

Badr


1 Answers

If you look at your thread dump, the threads are waiting on the task queue for more tasks. I.e this is the expected behavior.

The JVM exits only when all non-daemon threads have terminated, and thread pool uses non daemon threads.

You should call the shutdown() method on the Thread Pool to shut it down. You can call this method immediately after submitting tasks, as the already submitted task will be processed before shutting down (unlike the shutdownNow method).

Alternatively, it is also possible to have a thread pool that uses daemon threads. These will automatically shutdown when the last non daemon thread exits.

But in your case, you want I use the shutdown method. You can also call the awaitTermination method after calling shutdown to wait for your tasks to finish, in order to print a log message etc.

like image 97
Enno Shioji Avatar answered Jul 04 '26 18:07

Enno Shioji