Home arrow static arrow Java Programming [Archive] - finding out what method is interrupting a thread
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - finding out what method is interrupting a thread
This topic has 12 replies on 1 page.

Posts:31
Registered: 9/7/00
finding out what method is interrupting a thread  
Aug 6, 2004 9:11 AM



 
I have a method that first tries to retrieve an HTML page from a database cache, and failing to do so, retrieves the page from the web instead. In my main application thread, I spawn a new Thread to run this retrieval code, and have my main thread wait up to 60 seconds for it to finish using Thread.join().

Occasionally, I get an InterruptedException on the Thead.join(), but the stack trace gives me no clue as to which method did the interrupting, only on which thread was interrupted. Any ideas how to find this information out? Below is my code and the stack trace.

Thanks in advance.

  Runnable documentRetriever = new Runnable(){    public void run(){       //LOGIC HERE TO RETRIEVE PAGE FROM EITHER DATABASE OR WEB    }    }   Thread t = new Thread(documentRetriever, "DocRetriever");  t.setDaemon(true);  t.start();      try {    t.join(60000);  } catch (InterruptedException e) {      e.printStackTrace();  }


java.lang.InterruptedException   at java.lang.Object.wait(Native Method)at java.lang.Thread.join(Thread.java:1016)   at com.fetch.wrap.document.request.DocumentRequest.getDocumentSingleAttempt DocumentRequest.java:359)at com.fetch.wrap.document.request.DocumentRequest.getDocument(DocumentRequest.java:305)at com.fetch.wrap.document.request.DocumentRequest.getDocument(DocumentRequest.java:269)at com.fetch.wrap.document.request.DocumentRequest.getDocument(DocumentRequest.java:247)at com.fetch.wrap.execution.AgentExecution.fetch(AgentExecution.java:978)at theseus.operators.OpTest_fetch.on_request_binding_sets(OpTest_fetch.java:147)at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:324)   at theseus.executor.multi.WorkObject.run(WorkObject.java:77)
 

Posts:826
Registered: 7/1/03
Re: finding out what method is interrupting a thread  
Aug 6, 2004 9:28 AM (reply 1 of 12)



 
} catch (InterruptedException e) {    System.err.println(Thread.currentThread().toString());    e.printStackTrace();}

Ok, so I didn't try it, but that's where I would start :)

/k1
 

Posts:31
Registered: 9/7/00
Re: finding out what method is interrupting a thread  
Aug 6, 2004 10:13 AM (reply 2 of 12)



 
Thanks for your thoughts. However, the problem with that solution is that Thread.currentThread() will just return the Thread handling the exception, which is the thread that was interrupted. So again, I will not be getting information about the thread doing the interrupting.

Here is a small sample program that demonstrates the problem, with the output following. Basicly, I would like to get information about the Interrupter thread when I handle the exception.

  public static void main(String[] args) throws Throwable{    final Thread mainThread = Thread.currentThread();    System.out.println("Main thread = " + mainThread.toString());        Runnable longRunnable = new Runnable(){      public void run(){        try{ Thread.sleep(10000); } catch(InterruptedException e){}      }    };    Thread longProcess = new Thread(longRunnable, "LongProcess");      longProcess.setDaemon(true);       Runnable interrupterRunnable = new Runnable(){      public void run(){        mainThread.interrupt();      }    };    Thread interrupter = new Thread(interrupterRunnable, "Interrupter");       System.out.println("STARTING!");    longProcess.start();          interrupter.start();    try {        longProcess.join(3000);      }     catch (InterruptedException e) {         System.out.println("INTERRUPTED!");      System.out.println("Current thread = " + Thread.currentThread().toString());      e.printStackTrace();      }            System.out.println("FINISHED!");  }


This is the output:

Main thread = Thread[main,5,main]
STARTING!
INTERRUPTED!
Current thread = Thread[main,5,main]
FINISHED!
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Thread.join(Thread.java:1016)
at test.Test.main51(Test.java:85)
at test.Test.main(Test.java:59)
 

Posts:826
Registered: 7/1/03
Re: finding out what method is interrupting a thread  
Aug 6, 2004 10:55 AM (reply 3 of 12)



 
Well, that will teach me to post code that I even declared as untested!

But here's a question for you, that would indeed expand my personal understanding:

When would a thread be interrupted without an explicit call from another thread or thread group?

If never, then you could log the interrupt call from there...

...note how I refrained asking why you wanted to do this. :)

/k1
 

Posts:31
Registered: 9/7/00
Re: finding out what method is interrupting a thread  
Aug 6, 2004 11:22 AM (reply 4 of 12)



 
When would a thread be interrupted without an explicit
call from another thread or thread group?

I assume there is an explicit call. However, the interrupt may be coming from a third-party library that I did not code, or from code in the Java platform itself. In fact, in my case, that is almost certainly the case.
 

Posts:24,036
Registered: 2/3/03
Re: finding out what method is interrupting a thread  
Aug 6, 2004 11:24 AM (reply 5 of 12)



 
When would a thread be interrupted without an
explicit
call from another thread or thread group?

I assume there is an explicit call. However, the
interrupt may be coming from a third-party library
that I did not code, or from code in the Java platform
itself. In fact, in my case, that is almost certainly
the case.

I don't see a way you can do this, as you don't have a reference to the caller and no way to get one.
 

Posts:31
Registered: 9/7/00
Re: finding out what method is interrupting a thread  
Aug 6, 2004 12:03 PM (reply 6 of 12)



 
I was hoping that InterruptedException contained the reference, but that doesn't seem to be the case.
 

Posts:18,384
Registered: 21.03.00
Re: finding out what method is interrupting a thread  
Aug 6, 2004 3:02 PM (reply 7 of 12)



 
Hi,

You could always try to extend Thread, and override the method:
 public void interrupt();


And in that method print out current thread, before calling superclass.

/Kaj
 

Posts:31
Registered: 9/7/00
Re: finding out what method is interrupting a thread  
Aug 6, 2004 4:43 PM (reply 8 of 12)



 
That is a neat thought. Unfortunately, I am not neccesarily the creator of the Thread being interrupted. For example, in my simple program posted earlier, it is not the longProcess Thread that is being interrupted, but rather the main Java thread. In general, the code I am writing may be invoked on any Thread, most likely a Thread created by a Servlet container.
 

Posts:37,103
Registered: 3/30/99
Re: finding out what method is interrupting a thread  
Aug 6, 2004 4:55 PM (reply 9 of 12)



 
I wonder if a profiling tool might be able to help you, or a debugger. I've never used one this way, nor do I even know if any of them offer such a capability. Given the lower level access they have to the runtime though, it seems like a possibility.
 

Posts:37,103
Registered: 3/30/99
Re: finding out what method is interrupting a thread  
Aug 6, 2004 4:56 PM (reply 10 of 12)



 
Otherwise maybe look into JPDA.
 

Posts:826
Registered: 7/1/03
Re: finding out what method is interrupting a thread  
Aug 6, 2004 5:04 PM (reply 11 of 12)



 
If we agree that it's a third party library doing this, i.e. explicitly calling interrupt() or doing something equally specious (though I surely wonder why that library should and not report why it's doing it), then it's definitely time for a thread analyser tool.

I've used JProbe Threadalyzer effectively in the past to catch all sorts of nasty data races/deadlock conditions. Could be the medicine here also. I believe there's other similar tools out there that will do that job also.

Good luck with it, g.

/k1
 

Posts:31
Registered: 9/7/00
Re: finding out what method is interrupting a thread  
Aug 8, 2004 12:54 AM (reply 12 of 12)



 
Update: I found the problem.

Despite my earlier reservations, I realized I am executing this code in a Thread pool that I have created, and therefore I do have access to the invoking Thread. Thank you, kajbj, for the excellent suggestion of overriding interrupt(). I did this with the Thread in my ThreadPool, and much to my embarrasment, found that the invoker of the interrupt() method is in my code, not some 3rd party library I was thinking about blaming.

Thanks to everyone for all of the suggestions.
 
This topic has 12 replies on 1 page.