• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.interrupt() method is not returning!

 
olivieri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does any one know under which circumstances, if any, the java.lang.Thread.interrupt() method would not return? I thought this would never happen but it's actually happening in our code. Basically, our main thread in the program calls the interrupt() method of the other user threads that are also executing before terminating the application. The main thread does not continue with its exceution after calling the interrupt() method of another user thread. It just blocks for no apparent reason. Any clues? Thanks.
Here's a snippet of the simple code. activeThreads is an array of threads that were created ny the main thread:
for (int i = 0; i < activeThreads.length; i++) {
activeThreads[i].interrupt();
InformationLog.log(activeThreads[i] + " was interrupted.");
}
After the first invocation of activeThreads[i].interrupt(), the main thread is not returning.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that it's throwing an Exception - or even an Error - which is being caught and ignored somewhere in once of the calling methods? Try putting in a catch (Throwable t) to see if there's something like that going on. If there is, first find whatever code was hiding the exception, and fix that. Then fix the cause of the error itself.
 
olivieri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim - Thanks for your reply. I went ahead and added the try-catch block statement around the interrupt() call. However, no exceptions are being thrown. The main thread seems to "hang" for no apparent reason. If you have or anyone has some more ideas, please let me know.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roliv- looks like I overlooked your display name in my previous post. Please check out our display name policy and modify your display name accordingly (i.e. add a last (or first?) name). Thanks.
As for your question - I don't have any other explanation so far. As an experiment though, you might try first printing the name of each thread in your array, possibly along with other attributes like isAlive() and getPriority(). Try doing this for all thread before you interrupt any of them, just to get a complete list. Then try interrupting threads in a different order. Is there just one thread that blocks when you interrupt it, or do they all do this? If it's just one thread, maybe you can figure out what that one thread is doing. Or maybe it's OK to skip that thread entirely. If it's all threads, then there may be something funny about how threads are implemented on your machine. What OS are you using?
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that the interrupted thread goes into a loop upon being interrupted, keeping the interrupting thread from being scheduled again?
It isn't obvious to me what could cause this.
People often don't do the right thing with InterruptedException, but I think the the automatic clearing of the flag in this case protects us from a loop caused by ignoring the interrupt. I suppose there is room to get in trouble with isInterrupted().
Wonder what would happen if an InterruptedException catcher or whoever detected interrupted() invoked interrupt() on the current thread, and then when to the next iteration of its loop. Might that create a tight loop that could lock out whoever initially invoked interrupt()? (Just speculation, not something I've seen happen.)
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant think of any reason for it, it just sets a bit. I also suspect a security exception. Or perhaps their is some trouble loading the Security Manager?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see "roliv" is now "olivieri". Ummm.... we want a first and a last name. A first initial is acceptable (as in CL Gilbert, who kindly provided two ).
I'm thinking that if the interrupted thread is sufficiently higher-priority than the one doing the interrupting, and the interrupted thread goes into an infinite loop for some reason immediately after being woken, then some systems may never get around to letting any other threads be active. They're not required to, if a higher-priority thread is available (not waiting, sleeping, or blocking.) Are thread priorities being used in this code?
Where exactly did activeThreads come from? If you used the Thread.enumerate() method, that could be your problem. That gives you all active threads - not necessarily limited to the ones created from your main() method. JVM's often create threads of their own, as part of their underlying implementation. The thread being interrupted might itself be part of the thread-management system on your platform. It's probably a bad idea to just grab all active threads and interrupt them. You should really only interrupt threads that you yourself have created, that you know what they're supposed to be doing. I suggest maintaining a static List of all your threads, so that you can iterate through that list. (Of course, if that's what you did in the first place, then I guess it's back to my previous paragraph...)
[ June 24, 2002: Message edited by: Jim Yingst ]
 
Donal Lynch
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that the main thread does indeed return from interrupt() but gets stuck in the InformationLog.log() method because of, for example, a lock within the logging package that wasn't properly released on interrupt? I'm sure the answer is no, but it's a possibility to eliminate if you're stuck... Good luck.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was the Thread actually executing before you attempted to interrupt it?
M
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your thread might be blocking on IO, and thus un-interruptable(pre jdk 1.4). Are you doing any IO processing in the Thread you're trying to interupt?
M
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!