wood burning stoves 2.0*
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 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Shutting down an echoserver" Watch "Shutting down an echoserver" New topic
Author

Shutting down an echoserver

Reiner Herman
Greenhorn

Joined: Jan 12, 2010
Posts: 19
Hallo.

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
Greenhorn

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
Sheriff

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

Joined: Apr 15, 2009
Posts: 63
Hey.
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.

Adam


SCJP, SCWCD, SCBCD, SCDJWS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shutting down an echoserver
 
Similar Threads
Problem with echo server and client.
What's wrong here!
Client Applet- server application
reading from TCP socket problem
simple socket programming