简体   繁体   English

由于资源限制,JVM 是否有能力杀死线程?

[英]Has JVM capability to kill threads because of resource constraint?

I have created priority based threads ex-我已经创建了基于优先级的线程前 -

Thread Priority线程优先级

  • T1 P1 T1 P1
  • T2 P1 T2 P1
  • T3 P2 T3 P2
  • T4 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 .我对所有具有相同优先级的线程使用java.util.concurrent.CountDownLatch首先同时执行,然后同样继续执行下一个优先级线程。 Sometimes my application hangs, on seeing the thread dump I am observing all threads are in either Runnable or Parking state.有时我的应用程序挂起,在看到线程转储时,我观察到所有线程都处于 Runnable 或 Parking 状态。 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? JVM 是否有可能在执行之间以 P1 杀死线程,因此不会发生闩锁倒计时,并且 P2 优先级线程永远不会执行?

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.可接受的方法是向 Tread 发送一个信号以使其死亡,然后线程本身应该清理并终止。 Seehere methods interrupt() , interrupted() and isInterrupted() of class Thread.请参见此处的Thread 类的方法interrupt()interrupted()isInterrupted() 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.这意味着您自己的 Thread 实现应该检查它是否收到中​​断,如果是,则终止自己并进行所有所需的清理。

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? JVM 是否有可能在执行之间用 P1 杀死线程,因此不会发生闩锁倒计时,因此 P2 优先级线程永远不会执行?

No.不。

The JVM does not spontaneously kill threads in response to load or for any other reason. JVM 不会因负载或任何其他原因而自发地终止线程。 (Not even when the JVM is exiting. In that scenario, all extant thread go away when the JVM finally exits, or is killed.) (即使在 JVM 退出时也不会。在这种情况下,当 JVM 最终退出或被杀死时,所有现存的线程都会消失。)

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;他们可能只是以正常方式终止; ie returned from their run() method.即从他们的run()方法返回。

  • 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.如果允许异常从run()调用传播出去,线程将被终止。 You can configure an uncaught exception handler to deal with this;您可以配置一个未捕获的异常处理程序来处理这个问题; see the Thread javadocs for details.有关详细信息,请参阅Thread javadoc。

  • In some circumstances, calling Thread.interrupt() from one thread can trigger an exception in another thread.在某些情况下,从一个线程调用Thread.interrupt()可能会在另一个线程中触发异常。

  • It is theoretically possible that something has called the deprecated Thread.stop() method.从理论上讲,某些东西可能调用了已弃用的Thread.stop()方法。 (If your code is doing this, it is shouldn't. Fix it!) (如果您的代码正在执行此操作,则不应该。修复它!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM