Destroy the thread groups properly when shutting down.
authorFredrik Tolf <fredrik@dolda2000.com>
Tue, 13 Oct 2009 17:33:53 +0000 (19:33 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Tue, 13 Oct 2009 17:33:53 +0000 (19:33 +0200)
Are thread groups never garbage collected? That does seem too weird to
be true, but I haven't managed to get them collected so far.

src/dolda/jsvc/ThreadContext.java

index f21d04c..78a5d4e 100644 (file)
@@ -24,9 +24,25 @@ public class ThreadContext extends ThreadGroup {
     }
     
     public void shutdown() {
-       interrupt();
        if(root instanceof ContextResponder)
            ((ContextResponder)root).destroy();
+       try {
+           long last = 0;
+           while(true) {
+               long now = System.currentTimeMillis();
+               if(now - last > 10000) {
+                   interrupt();
+                   last = now;
+               }
+               Thread[] th = new Thread[1];
+               if(enumerate(th) < 1)
+                   break;
+               th[0].join(10000);
+           }
+       } catch(InterruptedException e) {
+           logger.log(Level.WARNING, "Interrupted while trying to shut down all service threads. Some may remain.", e);
+       }
+       destroy();
     }
     
     public RequestThread respond(Request req) {