Is there any way in Java to register * every * thread interruption?

I would like to somehow register every time Thread.interrupt() is called, recording which thread issues the call (and its current stack), and also identifies information about which thread is interrupted.

Is there any way to do this? Looking for information, I noticed that someone refers to the possibility of implementing a security manager. Is this something that can be done at run time (for example, in the Applet or Web Start client), or do you need to configure the installed JVM for this?

Or is there a better way to do this?

+6
java debugging multithreading interrupt
source share
5 answers

As a quick hack, it was much easier to do than I thought. Since this is a quick hack, I did not do such things as making sure that the stack trace is deep enough to dereference the array, etc. I pasted the following into my signed applet constructor:

 log.info("Old security manager = " + System.getSecurityManager()); System.setSecurityManager(new SecurityManager() { @Override public void checkAccess(final Thread t) { StackTraceElement[] list = Thread.currentThread().getStackTrace(); StackTraceElement element = list[3]; if (element.getMethodName().equals("interrupt")) { log.info("CheckAccess to interrupt(Thread = " + t.getName() + ") - " + element.getMethodName()); dumpThreadStack(Thread.currentThread()); } super.checkAccess(t); } }); 

and the dumpThreadStack method is as follows:

 public static void dumpThreadStack(final Thread thread) { StringBuilder builder = new StringBuilder('\n'); try { for (StackTraceElement element : thread.getStackTrace()) { builder.append(element.toString()).append('\n'); } } catch (SecurityException e) { /* ignore */ } log.info(builder.toString()); } 

I could never, of course, leave this in production code, but it was enough for me to say which particular thread calls interrupt() , which I did not expect. That is, with this code in place, I get a stack dump for every call to Thread.interrupt() .

+9
source share

Before you try something too wild, have you considered using the Java API debugging ? I think that capturing MethodEntryEvents on Thread.interrupt () could do this.

Eeek, that old interface, you should also check out the new "Interface" JVM Tool .

+4
source share

I would take a look at AspectJ and its capabilities for transferring method calls. The point pointcut around the interrupt() method should help here.

Note that since you want to intercept a call to a Java system method (unlike your application code), the above may not be acceptable. This thread seems to suggest this, but notice that he created the woven rt.jar.

+2
source share

As others have said ... If this is a one-time thing, JVMTI is probably the most hardcore way. However, it would also be fun to use the asm library and toolkit APIs to create an agent that inserts a call to the static method created just before the call to Thread.interrupt () (or perhaps modifies Thread.interrupt () to do the same I think you can do it).

Both take some time to learn, but enjoy working, and as soon as you master it, you can use them for all kinds of funny things in the future :-) I actually don't have good passages to insert here right now, but if you are google for ASM and maybe look at JIP for some creative use of ASM, I think you will find inspiration.

JIP: Java Interactive Profiler

+1
source share

You can also try using JMX:

 ManagementFactory.getThreadMXBean().getThreadInfo(aThreadID) 

with a ThreadInfo object you can register:

  • stream stack trace
  • general useful information such as name, status, etc.
  • etc.

EDIT

use getAllThreadIds () to get a list of live thread IDs:

 long[] ids = ManagementFactory.getThreadMXBean().getAllThreadIds(); 
0
source share

All Articles