• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread awoken prematurely

 
Haris Karameh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In thread object instantiated in WebSphere servlet the code goes:
public status=IDLE;
public void run(){
int cnt=3;
while (cnt>0)
try {
status=BUSY;
SOAPConnection con=...
...
cnt=0;
status=OK;
}
catch(Exception x){
if (--cnt==0){
status=FAILURE;
}
else {
try {
Thread.sleep(1500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Well, it is supposed to try 3 times to do the job and give up, waiting 1500ms in between tries.
Everytime it reachec sleep() it is 'Interrupted' by something right away.
What am I doing wrong and how to make it sleep?
TIA
Haris
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first: Please use code tags while posting code. Code is easier to read if you do that.

haris: Everytime it reachec sleep() it is 'Interrupted' by something right away.

This would typically happen in the following two cases:

  • If some code is actually interrupting the thread.
  • If the thread has been interrupted once and the interrupt flag is not cleared since then. The sleep method first checks whether the interrupted flag in set to true for the thread. If it is true then it straight away throws the InterruptedException and does not get into sleep.


  • I suspect the second case, so just before getting into the loop, can you call Thread.interrupted() and then check whether you get the same behavior.
    If yes, then some code is actually interrupting the thread.
    (I am assuming that you are getting an InterruptedException in the above code.)

    You can also do one thing. Override interrupt() method in the thread. Before calling super(), just print "interrupted" or something. This would tell you as to when is the interrupt called.
     
    Haris Karameh
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the reply.
    I'll try suggested. I am positive that I did not implement anything which would intentionally interrupt the tread. Maybe servlet container or some other part of WAS has to do something with it. The only thing which comes to my mind is SOAPConnection, which I did not close in the main loop.
    This is kind a hard to catch because it happens only when the main part fails.
    Thanks once again
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!