0

I am having a problem with my application since some of threads I create become stuck , I have a way to find out what are they , I am trying to write a process to stop these threads [ kill them ] from outside my application since it becomes unresponsive when the stuck threads count increases over time [ I tried to fix the root cause but seems that it sometimes happens because of database aquire connection lock ]

Is there any way to access the JVM from outside I tried com.sun.tools.attach.VirtualMachine but it didn't work is there any other way

Regards,

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
AhMaD AbUIeSa
  • 805
  • 1
  • 12
  • 21
  • This sound like quite dirty workaround. I would suggest implementing some mechanism to kill (e.x. timeout) the thread inside the process that starts it. – Lemonov Jul 04 '17 at 11:48
  • I must support Lemonov's comment, you should fix you application rather than cleaning up it's crap with an extra garbage collector. Very bad practice. Find the root of your freezing threads, fix it, enjoy. – Luftbaum Jul 04 '17 at 11:52
  • well , I know and as I said I can't control the thread behavior , and at what step in the process the thread comes stuck is not clear and not fixed @Lemonov – AhMaD AbUIeSa Jul 04 '17 at 11:58
  • Se you can't modify source of the problematic application?@AhMaDAbUIeSa – Lemonov Jul 04 '17 at 12:06
  • Anyway it seems to be possible but it varies depending on OS. Here related topic (Windows): https://stackoverflow.com/questions/22441878/attach-to-already-running-jvm. – Lemonov Jul 04 '17 at 12:12
  • The OS that I am using is Linux redhat, and if I can do that using java would be better [ the described solution is C++ ] @Lemonov – AhMaD AbUIeSa Jul 04 '17 at 12:13
  • You can try killing/stopping the thread by simply getting pid of thread and sending proper signal to it but i can not guarantee that it would not kill/stop also the parent pid. You can achieve even from linux shell. – Lemonov Jul 04 '17 at 12:25
  • @Lemonov , what I have as a process is the main application java pid [ I can't get the thread id ] – AhMaD AbUIeSa Jul 04 '17 at 12:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148318/discussion-between-lemonov-and-ahmad-abuiesa). – Lemonov Jul 04 '17 at 12:39
  • `jstack` will list all Java threads with stack traces and with the corresponding native thread id (`nid`), see [here](https://stackoverflow.com/a/26550955/3448419) – apangin Jul 04 '17 at 13:00
  • @Lemonov ok when I get the output how can I use the output to stop or send a signal to that process to quit or stop [ here talking about a thread ] – AhMaD AbUIeSa Jul 04 '17 at 13:09
  • @apangin , and what can I do with nid [ can I use kill or a process to abort the thread ] – AhMaD AbUIeSa Jul 04 '17 at 13:17
  • You cannot kill a single thread in Linux. Even if you could, this would not help, since the resources/locks acuired by the thread would not be released. There must another way to solve your problem, e.g. by setting connection timeout or something. – apangin Jul 04 '17 at 17:28
  • This sounds like a bad bodge to me:( As others have suggested - fix the root cause. – ThingyWotsit Jul 04 '17 at 19:41
  • @apangin tried that [setting the timeout ] , but once the thread is stuck no way to handle that according to the environment – AhMaD AbUIeSa Jul 05 '17 at 03:59
  • I never tried it but take a look into JMX http://docs.oracle.com/javase/7/docs/technotes/guides/management/agent.html VisualVM also uses the JMX to monitor other JVM's and do monitoring of local and remote machines (you can access and capture thread information and thread dump too) – iswan Jul 05 '17 at 14:55
  • @swanandingawale I am trying to terminate the thread that I already know all the information regarding the threads , tried the JMX [like] on my environment setup , and found nothing to help my case – AhMaD AbUIeSa Jul 06 '17 at 04:22

0 Answers0