From a0b186f8f8342791c1d65ca8f04118b0d73d5bb8 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Tue, 13 Oct 2009 19:33:53 +0200 Subject: [PATCH] Destroy the thread groups properly when shutting down. 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 | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/dolda/jsvc/ThreadContext.java b/src/dolda/jsvc/ThreadContext.java index f21d04c..78a5d4e 100644 --- a/src/dolda/jsvc/ThreadContext.java +++ b/src/dolda/jsvc/ThreadContext.java @@ -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) { -- 2.11.0