Hi,
I have a serversocket running as a thread ( i.e. socket is listening continuously in the run method of that thread).
I am not able to invoke thread.interrupt on that thread. Can somebody suggest a way to stop that thread?
Thanks
Puru
-
unable to use thread.interrupt (4 messages)
- Posted by: Puru Kk
- Posted on: March 06 2003 18:56 EST
Threaded Messages (4)
- ServerSocket by stuart skinner on March 07 2003 09:46 EST
- ServerSocket by stuart skinner on March 07 2003 09:48 EST
- ServerSocket by Puru Kk on March 07 2003 15:52 EST
- ServerSocket by stuart skinner on March 10 2003 05:10 EST
- ServerSocket by Puru Kk on March 07 2003 15:52 EST
-
ServerSocket[ Go to top ]
- Posted by: stuart skinner
- Posted on: March 07 2003 09:46 EST
- in response to Puru Kk
Close the server socket and fall out of the run method.
class Server implements Runnable
{
ServerSocket socket;
void run()
{
try
{
while(true)
{
handle(socket.accept());
}
}
catch(SocketException e)
}
} -
ServerSocket[ Go to top ]
- Posted by: stuart skinner
- Posted on: March 07 2003 09:48 EST
- in response to Puru Kk
Missed a bit
Close the server socket and fall out of the run method.
class Server implements Runnable
{
ServerSocket socket;
void run()
{
try
{
while(true)
{
handle(socket.accept());
}
}
catch(SocketException e)
}
void shutdown()
{
sock.close();
}
} -
ServerSocket[ Go to top ]
- Posted by: Puru Kk
- Posted on: March 07 2003 15:52 EST
- in response to stuart skinner
thanks. I am attaching the skeleton code which I used... please let me know if I am doing any error
/////////////////////////////
class server implements Runnable {
/**
* Main processing method for the server object
*/
public void run() {
try {
while (true) {
//open socket and keep listening and process the received messages for ever
}
} catch (InterruptedException e) {
// somebody interrupted the thread.. stop the socket and end
socket.close;
e.printStackTrace();
return;// ends this thread
}
}
/**
* The main program for the server class
*
*@param args The command line arguments
*/
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup(
"server group");
server p = new server();
serverp1 = new server();
Thread worker = new Thread(group, p);
Thread worker1 = new Thread(group, p1);
worker.setName("worker thread");
worker.setPriority(1);
worker.start();
worker1.start();
group.interrupt();// should shutdown the server...
// its shutting down the server I do not use socket... from the
definition of interrupt, it will interrupt interruptablechannel,wait,sleep etc
somehow I am not able to get this thing to interrupt a thread in which a socket
is waiting for connections
}
}
///////////////////////////////// -
ServerSocket[ Go to top ]
- Posted by: stuart skinner
- Posted on: March 10 2003 05:10 EST
- in response to Puru Kk
It will only cause this to happen if you are using an InterruptableChannel from the new nio stuff. I don't think that using the ServerSocket straight you are doing this. Try using a ServerSocketChannel instead. This can be obtained by creating a standard ServerSocket and calling getChannel. accept() on ServerSocketChannel will throw a ClosedByInterruptException - If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status.
Hope this works
Stu