Home arrow static arrow Java Programming [Archive] - how to stop thread that is waiting
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how to stop thread that is waiting
10 Duke Stars available
This topic has 6 replies on 1 page.

Posts:22
Registered: 7/2/02
how to stop thread that is waiting  
Jun 13, 2004 4:47 AM



 
well here is mu code:
import java.io.*;
import java.net.*;

public class PodserverEngine extends Thread{
public static int port=8282;
public ServerSocket mySocket;
boolean kraj = false;
public Socket socket;
public PodserverEngine(PodserverMain main) {

this.main = main;
}

public void run(){
try{
mySocket = new ServerSocket(port);
while(!kraj){
socket = mySocket.accept();
new EngineThread(socket, this, 1, main).start();
}

}catch(Exception e){
handleException(e);
}
}

public void handleException(Exception e){};
public void initialize(){};
public void end()throws Exception{
mySocket.close();
socket.close();

kraj=true;

}

}
well my problem is that I can't stop this thread calling end() couse I suppose that it doesn't exit run method becouse it keeps waiting on mySocket.accept() so does anybody knows how to stop it, just to say that I have deleted all references to this thread so that is not the problem. I also tried first to put thread at sleep and then called interrupt but it doesn't work. Does anybody know how I can break waiting on mySocket.accept() or another way to stop this thread?

 

Posts:6,147
Registered: 11/9/00
Re: how to stop thread that is waiting  
Jun 13, 2004 4:59 AM (reply 1 of 6)



 
What happens if you hit the thread with an interrupt while in accept?

In theory it ought to through an InterruptedIOException.
 

Posts:22
Registered: 7/2/02
Re: how to stop thread that is waiting  
Jun 14, 2004 6:45 AM (reply 2 of 6)



 
nothing happens it is blocked on mySocket.accept() and waits here, couse you can interrupt thread only when thread is at sleep(). I have solved this problem by putting mySocket = null (in method end()) and that throws exception in run method of the thread, but maybe thats not good idea.
 

Posts:759
Registered: 8/30/01
Re: how to stop thread that is waiting  
Jun 14, 2004 6:54 AM (reply 3 of 6)



 
Try extending ServerSocket.
Then override the accept method to have boolean flag to let you know that the accept went though.
Have your Thread merely check for this flag instead of waiting for the accept to return.
 

Posts:37,103
Registered: 3/30/99
Re: how to stop thread that is waiting  
Jun 14, 2004 6:54 AM (reply 4 of 6)



 
Use [url http://java.sun.com/j2se/1.4.2/docs/api/java/net/ServerSocket.html#setSoTimeout(int)]setSoTimeout[/url] and then when you catch the timeout exception, check for a "done" flag being set. If it's not set, then you continue your accept loop. If it is set, you're done.

It won't cause it to close down immediately, but if you pick you timeout small enough, it won't be a long wait but it neither will it be a spin lock. I'd suggest anywhere between 100 ms. and 60 sec.

 

Posts:37,103
Registered: 3/30/99
Re: how to stop thread that is waiting  
Jun 14, 2004 6:56 AM (reply 5 of 6)



 
Try extending ServerSocket.
Then override the accept method to have boolean flag
to let you know that the accept went though.
Have your Thread merely check for this flag instead
of waiting for the accept to return.

Huh?
 

Posts:22
Registered: 7/2/02
Re: how to stop thread that is waiting  
Jun 16, 2004 5:57 AM (reply 6 of 6)



 
I got problem solved, here is what I have missed: when I call in method end() mySocket.close() the same moment it throws exception in run method on mySocket.accept() but it doesn't finish method end() and kraj=true is never reached so thread just stops here and exits run() method, so I just added in catch part few lines to finish everything (eg. kraj=false)
 
This topic has 6 replies on 1 page.