• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shutting down an echoserver

 
Reiner Herman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for "beginning Java". Moving thread.
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic