Thread problems - shutting down.

10-11  Source: Network gathering  Views:0 

Advertisement
Okay, I've got my multi-threaded server program working just fine and all is well...until I shut down the server.
By some quirky twist of fate, java chat servers seem to be a popular education assignment in the last two weeks, which will probably put some people off answering this simply because of all the clueless posts on the subject recently. :)
Anyway, a little background on how I'm doing things first.
At the top there is the Server object, which is implementing Runnable. This instances the ClientManager, creates the ServerSocket and then kicks off its own thread and loops in the run() method waiting for connections while a boolean is true. If it gets a connection it calls the ClientManager addConnection() method and passes it the received client socket.
The ClientManager holds a List of ClientConnections and provides methods to add and remove ClientConnections. The ClientManager also runs in its own thread by implementing the Runnable interface and uses its run() method to loop through checking for incoming messages from each client.
A ClientConnection object also runs in its own thread via the Runnable interface. This checks for incoming messages from the client and stores the String ready to be received by the ClientManager and broadcast to all ClientConnections.
So, you've got the Server running in its own thread, the ClientManager running in its own thread and the ClientManager maintaining a List of ClientConnections, each running in its own thread.
I'm starting my threads like this:-
// Constructor.
Public SomeObject() {
    start();
// Runnable method.
Public void start() {
    // threadObject is private class variable.
    bThreadActive = true;
    this.threadObject = new Thread(this);
    threadObject.setDaemon(true);
    threadObject.start();
}I'm running my threadded object like this: -
Public void run() {
    do {
        // Do work here.
    } while(bThreadActive);
    // Activates the shutdown method for this object.
    shutdownObject();
}I'm shutting down my threads like this: -
Public void shutdown() {
    this.bThreadActive = false;
// The main shutdown code.
Public void shutdownObject() {
    // Kill thread object.
    this.threadObject = null;
    // Do other stuff.
}Which I think should work fine. Anything wrong with the above?
I'm getting problems with NullPointerExceptions with the Server object at the point where it tries to shutdown the ClientManager. This works by shutting down all ClientConnections in the List first and then emptying the List before killing its own thread. Also, the Server object seems to shutdown before the ClientManager. And other such weirdness.
I know you're all going to ask for specific code and a stacktrace, which I'll provide....I just want to check that my method for using threads as above is correct first, so I can rule that out - mostly because I suspect I'm missing some vital piece of knowledge on using threads....things are not working as I expect.
Anyway, a quick answer on the above and then I'll start posting more specific info.
Thanks all :)
Ooops....my mistake. I was calling shutdown() twice. Once from the GUI code and automatically from after the loop in the run() method. Funny how the stacktrace doesn't drill down any further than the method that makes this call.
Okay, its almost all working perfectly, except for this part....
    //  Start accepting client connections from the server service.
    public void run() {
         while(bThreadActive) {
             if (sockServer != null) {
                 if (!sockServer.isClosed()) {
                     try {
                         sockClient = sockServer.accept();
                         if (sockClient != null) {
                             this.clientManager.addClientConnection(sockClient);
                             this.pOutput.printOutput("Connection accepted from: " + sockClient.getInetAddress().getHostAddress());
                     } catch (IOException ioe) {
                         if (bThreadActive) {
                             this.pOutput.printOutput("ERROR: Could not accept incoming client connection");
                     } catch (NullPointerException npe) {
                         // Leave this for now.
         shutdown();
    // Shutdown the server service and disconnect all client connections.
    public void shutdown() {
        try {
            clientManager.stopThread();
            sockServer.close();
            threadServer = null;
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        } catch (IOException ioe) {
            pOutput.printOutput("ERROR: Could not cleanly shutdown the server socket");
        pOutput.printOutput("Server service shut down successfully");
    // Stop the server thread (automatically shuts down).
    public synchronized void stopThread() {
        bThreadActive = false;
    }When I call the above like this from the GUI object...
    // Stop the server.
    private void stopServer() {
         try {
              server.stopThread();
              setGUIMode(MODE_DISCONNECTED);
         } catch (NullPointerException npe) {
              npe.printStackTrace();
    }...the string "Server service shut down successfully" from the completion of the shutdown() method never appears, so it looks as though its not executing.
But if I do this instead in the stopServer() method and remove the shutdown() call in the run() method, it works......but I get two (sometimes three) outputs of "Server service shut down successfully" which makes me think its being run twice somehow.....once before the ClientManager has been shutdown and once after or twice after.
Server service shut down successfully
Shutting down Client Manager...
All client connections disconnected
Client Manager successfully shutdown
Server service shut down successfully
Server service shut down successfully
Any ideas?
Related articles
  • Thread problems - shutting down. 10-11

    Okay, I've got my multi-threaded server program working just fine and all is well...until I shut down the server. By some quirky twist of fate, java chat servers seem to be a popular education assignment in the last two weeks, which will probably put

  • Problems shutting down laptop 11-30

    Good morning!! I have a Toshiba Satellite P75-A7200 and sometimes I have problems shutting down it. I have seen in some forums that it can be because I have open apps. After checking it out I see that all of them are closed and I cannot close the lap

  • Child process admin thread is shutting down. 11-30

    Hi, Operating on a web server with the following error message, Child Process is a phenomenon that restart. I would like to know what the cause. Version - Sun Java System Web Server 6.1 errors [09/Nov/2011:12:31:00] catastrophe ( 7647): Server crash

  • Black screen on starting up in OSX, problems shutting down in XP. 10-11

    Hi there. I have a 2-month old iMac with Bootcamp and Win XP installed. It ran OK for a while, but Bootcamp has started behaving erratically. I did postst this in the Bootcamp part of discussions, but have come to the decision that it is not a Bootca

  • Problems shutting down and starting up 10-11

    my MacBook Pro (OSX 10.9.3) doesn't shut down when I want it to but goes into a coma periodically. when I use "Shut Down" every once in a while via Apple menu, the machine doesn't appear to actually shut down. when I open it or got to it the nex

  • Problems shutting down... 10-11

    50% of the times i try to shut down, my computer quits all the open applications and then just gives me the spinning rainbow and does nothing. i eventually just manually turn it off with the button in the back. any advice on fixing this problem? also

  • Problems shutting down a 6534 11-30

    Hi, I have searched the forums and couldn't find this sorry if its a rehash. I reciently built a box with a 6534 and a celery 1.7. Everything is fine it runs fine, doesn't heat up, actually very stable. Except... I can't shut it down. It will shut do

  • Problems shutting down illustrator cs3 11-30

    greetings - just curious if anyone else has had issues w/ illustrator not shutting down (i.e., file/exit). i have yet to attempt an uninstall/reinstall since it took a long time to install cs3 on my machine the first time (had to restart it once). ab

  • Problems shutting down, starting up 11-30

    My IMac has been giving me several problems lately. It sometimes will not go to sleep, and when it does, will not wake up. I have to re-start the computer to wake it up. It often shuts down with no warning or the grey film comes over and says the Mac

  • Problems shutting down - just starts up again! 11-30

    Hi When I try to shut my macbook down, it just bounces back to life again. Very strange. It only started doing this today (I don't think I installed anything new this morning). Everything on the desktop, incl. dock goes away but then it comes back ag

  • Problems shutting down and getting 'Error -39 when trying to open my files 11-30

    After working all evening using my Macbook Pro, I close out all my programs and shut down the computer. However, the computer refuses to shut down. The screen freezes and just sits there. After about 10 minutes of waiting I eventually hold down the p

  • 0xE8000065 error and problems shutting down 11-30

    I recently tried to shut down my iPhone 4 and it is stuck on the shut down screen... I then tried to plug it into my windows computer and the error above showed... If all else fails i am gonna wait for the battery to die, it was at about 40% but i ha

  • Problems shutting down - reset PRAM? 11-30

    Hi The last two evenings I've come to turn on my macbook (1.83 GHz) pressing the on button hasn't worked. The first time, I had previously left something plugged into one of the usb's so assumed I'd run the battery down. Plugging the macbook in allow

  • IPlanet 6.1-SP6 auto re-starting with Child admin thread shutting down 11-30

    We are running the same application everywhere without any issues. Just migrated to new machine/environment and whenever the client (browser) tries to acess a JSP, the web-server automatically re-starts itself with message Child admin thread is shutt

  • My mac hangs before shutting down 10-11

    Hello After installing Leopard on my G5 I have problems shutting down The system as always closes all applications, the menu bar disappears, the grey weel starts to rotate but then nothing happens. It keeps rotating forever. I tried resetting the RAM

  • OS won't shut down - spinner goes around and around 10-11

    For the last month or so my Mini will not shut down (or restart). I am on the latest rev. of SL (10.6.8). It gets to the blue screen and then the spinning wheel goes around and around until I have to turn it off by the power button. I found much olde

  • Can't shut down, some applications don't close and others don't open 10-11

    I installed Leopard about 3 weeks ago as a clean install not an update. Since then, I have frequent problems shutting down and con only do it manually. I also have some applications - like Firefox - which won't close - the blue light in the dock stay

  • T430s Takes Several Minutes to Shut Down 10-11

    Hey all, I've got a T430s running Windows 8.1. The machine runs great and boots very fast (probably helps that I bought an SSD); however, when I shut down, it takes several minutes (5+ minutes) to fully power-down. The screen goes black, but USB devi

  • ITunes causes Macbook Pro not to shut down 11-30

    Dears, For sometime now I am not able to shut down my mbp without holding the power bottom down. I tried so many things, restored lion (performance enhanced but still won't shot down), then i started deleting app thinking it could be the reason until

  • Satellite T130 keeps restarting instead of shutting down 11-30

    Just bought a refurbished Toshiba Satellite T130 laptop pre-installed with Windows 7, and since we got it, it has had a problem shutting down. When I press Shut Down, it shuts down ok, all the lights go off as if it just shut down, and then a couple