This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.interrupt not working properly

 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I use a ThreadPoolExecutor that executes some runnable on 10 threads. the code that has been executed is read some information from a socket server.
At some times I want a specific thread to be terminated so I use Thread.interrupt and it work in 90% of the cases. I know because afterExecute(Runnable run, Throwable t) method from the ThreadPoolExecutor is executed.
However in 10% of the cases the thread is not interrupted. I know because the method afterExecute is never executed and because ThreadPoolExecutor.getActiveCount() returns 1 thread ( the one that it supposed to be interupted).

I have to mention that I read information from a socket server and the connection is tunneled via a proxy(not sure if this is the problem)

Do you know any solutions to my problem?


Thanks
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just post about that the interrupt method actually doesn't interrupt. =)

One part of the documenation says that: If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

That's what I don't understand, because these methods (join, sleep) throw InterruptedException, so to run them you need to catch the exception (or propagate), hence the thread is not interrupted, and continue execution.

For instance, try the code below:


Uncomment the catch block and it will work.
I'm just learning threads, but I think this is not a correct behavior. The interrupt method should kill the thread, but this is not the case.
Is there some method to kill a thread?
 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Leandro. Thanks for your reply. Yes you are right , but this is not my problem. I already have a Thread.sleep(milliseconds) in my runnable and it doesn't throw anything.

here is some part of the code in my runnable



when I execute Thread.interrupt() it show me that the thread is interrupted ( returns to the console "thread interrupted on thread: Thread-1"). but the "sleep interrupted on thread: Thread-1" is not shown.





I think it has to do with the socket. I already set

socket.setConnectTimeout(CONN_TIMEOUT);
socket.setReadTimeout(CONN_TIMEOUT);

but with no success.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the time its interrupt() method is called, has the thread reached the sleep() statement yet?
 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:At the time its interrupt() method is called, has the thread reached the sleep() statement yet?



no. it's reading from the socket. if it reach the sleep than the interruption will work fine.
Also the socket is closed when the interrupt method is called on 90% of the cases. but there is a 10% cases which don't close the socket
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doua Beri wrote:here is some part of the code in my runnable




So it doesn't print: sleep interrupted on thread..., right?
Try this:

 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like your having problems with interrupting blocked IO and this is a well written on subject (google away ;-) ). I think the exact behaviour you get is OS dependent , Windows I believe is particularly unfriendly. I've seen written though can't confirm that you might be better closing the underlying socket to interrupt the thread or I believe you should use the newer non blocking api .

 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Hurst wrote:It sounds like your having problems with interrupting blocked IO and this is a well written on subject (google away ;-) ). I think the exact behaviour you get is OS dependent , Windows I believe is particularly unfriendly. I've seen written though can't confirm that you might be better closing the underlying socket to interrupt the thread or I believe you should use the newer non blocking api .




I think I managed to solve the problem. yes I thin you're right. That was my thought. I tested on linux with no problems. yes windows has some problems with closing sockets.

however I tried to close the socket and is working fine. however Thread.interrupt doesn't do the same thing?
"If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException."


Also what do you mean by newer non blocking api ???

Thanks
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"re: newer non blocking api . "

I got the impression you were using old style thread per socket java io rather than nio as that fitted your problem description apologies if that was an incorrect assumption ...

http://www.developer.com/java/article.php/3837316/Non-Blocking-IO-Made-Possible-in-Java.htm
 
Doua Beri
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Hurst wrote:"re: newer non blocking api . "

I got the impression you were using old style thread per socket java io rather than nio as that fitted your problem description apologies if that was an incorrect assumption ...

http://www.developer.com/java/article.php/3837316/Non-Blocking-IO-Made-Possible-in-Java.htm



thanks. that is something I missed. good info. but my problem actually was solved by closing the socket. Using this new method will not block my thread but still will not close the connection on windows OS and still some resource are being used.
But this is a good information. it helped me much.

Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic