File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Shutting down an echoserver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Shutting down an echoserver" Watch "Shutting down an echoserver" New topic

Shutting down an echoserver

Reiner Herman

Joined: Jan 12, 2010
Posts: 19

I have created an EchoServer extending Thread. I want it to be possible to shut down the server so I have the following in my code when overriding Thread's run():

and in a shutDown method:

Problem is this can throw IOException in run() because shutDown can be executed the condition in the while loop in run has been checked. If I synchronize on the EchoServer itself I lose all concurrency. What are possible strategies ?

Unrelated: I am assuming there is no direct way of getting references to all Sockets coming from a specific ServerSocket's accept, correct ?
Reiner Herman

Joined: Jan 12, 2010
Posts: 19
OK. Thinking about it I think I am suffering from Threadatitis ! That EchoServer probably should not be a thread.
So will use synchronization on some state variable isAlive probably and double check somehow. Sounds better, right ?
Double Checked locking works from Java 1.5 on, but is it considered bad to use ?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46429
Too difficult for "beginning Java". Moving thread.
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
I don't know the all your design and implementation. Neither your idea, but it seems not to work well.
Please, take a look at my example:

I hope it will turn out helpful for you.


I agree. Here's the link:
subject: Shutting down an echoserver
It's not a secret anymore!