Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem stopping Thread

 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic