It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes Problem stopping Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Problem stopping Thread" Watch "Problem stopping Thread" New topic
Author

Problem stopping Thread

Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
I am trying to stop a thread which listens for incoming socket data.





When doing this, I get the an IO exception when closing the sockets, even though closing them is done after stopping this thread.



Any ideas why this is happening? I thought the stop method would exit gracefully from the thread...

Thanks,
Sammie
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Any ideas why this is happening? I thought the stop method would exit gracefully from the thread...


What is happening is ... the stop flag is only checked when the readline() method returns, which in this case, is caused by an exception thrown when the socket is closed.

What you need to do is, in your catch block, is check to see if the stop condition is true. If it is, then you need to ignore the exception, don't print out the exception, and cleanup and exit gracefully.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
Thanks Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem stopping Thread
 
Similar Threads
Will it ever be GCed?
Socket Connection.. Was working, now it's not??
Socket design question
My Context Listener problem
Networking question