aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread Hanging on stop() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Hanging on stop()" Watch "Thread Hanging on stop()" New topic
Author

Thread Hanging on stop()

Stephane LeGros
Greenhorn

Joined: Jan 30, 2005
Posts: 10
I am having a problem with a thread which I am trying to stop, but which hangs on the stop. I have included the code here below.

Basically, I have a thread which does a read on a socket and just echos out whatever it read in. The client sends up an objet and then goes away. At which point the thread correctly sees that it's lost its partner and breaks out of its loop and calls the cleanup code, which is where it hangs.

Can anyone see what I'm doing wrong? Shouldn't the stop() interrupt the readObject() function? If not, how is this normally handled? I tried implementing a catch block for InterruptedException and having the cleanup code invoke an interrupt(), but my IDE (Eclipse) told me that readObject() doesn't throw InterruptedExceptions.

Don't see where I'm going wrong - any feedback greatly appreciated!

Here is the run() method for the thread:

public void run() {
stream = createInputStream();

while (true) {
try {
input = stream.readObject();
} catch (IOException e) {
if (e.getMessage().equals("Connection reset")) {
echo ("remote side went away.");
break;
}
JOptionPane.showMessageDialog(null, "Unhandled IOException in ReceiveThread.");
e.printStackTrace();
System.exit(-1);
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "Unhandled ClassNotFoundException in ReceiveThread.");
e.printStackTrace();
System.exit(-1);
}
JOptionPane.showMessageDialog(null, "Read in this: (" + input.toString() + ")");

}
echo ("have fallen out of main loop in ReceiveThread()");
getSocketManager().handleLostRemote();
}


And here is the method for doing the cleanup. The "hang" occurs on the stop() method call.

public void handleLostRemote(){
try {
getSocket().close();
echo ("close of socket was successfull.");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "While bringing down socketmanager, IOException.", "SocketManager", JOptionPane.OK_OPTION);
e.printStackTrace();
System.exit(-1);
}
echo ("stopping receiver thread... ");
getReceiver().stop();
echo ("have returned from stopping.");
}





stephane LeGros
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
First of all, you shouldn't be using the Thread.stop() method at all. It has been deprecated for good reason and there are strong warnings against its usage in the JavaDoc and here...


James Carman, President<br />Carman Consulting, Inc.
Stephane LeGros
Greenhorn

Joined: Jan 30, 2005
Posts: 10
Thank you for the link.

I will avoid using stop() and use other ways (mentioned in the link) for handling this.

Sorry - a bad question. Should have researched it more before posting online.



stephane LeGros
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Hanging on stop()