0

I have created priority based threads ex-

Thread Priority

  • T1 P1
  • T2 P1
  • T3 P2
  • T4 P2

I am using java.util.concurrent.CountDownLatch for all threads with same priority to execute simultaneously first then proceed with the next priority threads likewise . Sometimes my application hangs, on seeing the thread dump I am observing all threads are in either Runnable or Parking state. I have handled all the corner cases to do count down of the latch so that next priority threads execute.

Is it possible that the JVM kills the thread with P1 in between the execution so count down of latch does not happen, and the P2 priority threads never execute?

Harsh Vardhan
  • 111
  • 1
  • 4
  • The jvm doesn't just randomly kill threads. – matt Jan 27 '20 at 12:54
  • Please show the code, which uses threads – Steyrix Jan 27 '20 at 13:01
  • Does your thread-dump show that one of the threads has been killed/terminated or are they all still there? – matt freake Jan 27 '20 at 13:33
  • There is no way for JVM (or you as a developer) to kill a thread. You can `interrupt()` a thread but that only sets a flag. You may also count down your latch in a `finally` block to make sure it is executed in case of exceptions being thrown. – Ivan Jan 28 '20 at 04:48

2 Answers2

2

Killing Thread from outside was deprecated long ago because there is no clean way to clear up all thread resources from outside. Thus you are risking resources not cleaned up which results in memory leaks and other nasty side effects. The accepted way is to send a signal to a Tread to die and then thread itself should clean up and terminate. See here methods interrupt(), interrupted() and isInterrupted() of class Thread. This means that your own implementation of Thread should check if it received an interruption and if so terminate itself with all the cleanup required.

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

Is it possible that the JVM kills the thread with P1 in between the execution so count down of latch does not happen, so the P2 priority threads never executes?

No.

The JVM does not spontaneously kill threads in response to load or for any other reason. (Not even when the JVM is exiting. In that scenario, all extant thread go away when the JVM finally exits, or is killed.)

If threads that you expect to be there have gone away, there are a couple of possible explanations.

  • They may have simply terminated in the normal way; i.e. returned from their run() method.

  • They may have done something that triggered an exception. If the exception is allowed to propagate out of the run() call, the thread will be terminated. You can configure an uncaught exception handler to deal with this; see the Thread javadocs for details.

  • In some circumstances, calling Thread.interrupt() from one thread can trigger an exception in another thread.

  • It is theoretically possible that something has called the deprecated Thread.stop() method. (If your code is doing this, it is shouldn't. Fix it!)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216