• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cleaning up after a Multi-threaded Server

 
mpowell2
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
First let me give you an idea of the basic framework of my program. Then I'll let you know what I am having trouble with.
I have a GUI with a button, this button both starts and stops a server. It starts the server by creating the server object, then it creates a thread to use the server. So, this thread accesses the server.waitForConnection() method that uses the server socket to accept incoming connections. When a new connection is established, another thread is spawned to handle the connection and it goes back to accepting...
(I hope that is clear)
What I want: I want the user to be able to stop the server. Doing so cleans up everything and destroys the server object. The user can then start the server again and a new clean server is created.
What my problem is: How do I clean up all the client connections and threads for every client that connects (there can be any number of these), clean the server sockets and the thread running in the GUI, then clean up the reference to the server socket?
I am not familiar with Java garbage collection etc. So please any help would be greatly appreciated.
Thank you,
Mark
P.S. please e-mail me at mpowell@psl.nmsu.edu if something needs clarification.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mpowell2",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Yuri Gadow
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could register each thread as you create it, in a Set for instance, and then signal them when it is time to shutdown. The "signal" would simply be a method that sets a flag on the thread that is polled during its run. Alternatively, you can signal with interrupt() and poll with isInterrupted(). The server would be terminated in the same way.
A smaller implementation would be to have the server (which is spawning threads) register itself with the threads before their run starts, so that they can callback to poll a state on the server � depends on your size vs. speed requirements.
Once the thread completes its run, the thread resources (OS level) are deallocated and all that would remain is the reference you hold in the Set � which you will need explicitly clear out � using a weak reference would get you an unreachable thread. Once the branch of instances for a connection is unreachable, it will be eligible for GC.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!