• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cleaning up after a Multi-threaded Server

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic