JConsole leaks threads

"White, John K" <JohnWhite-1VmriVQ4F2F8UrSeD/[email protected]> Fri, 5 Nov 2004 12:52:41 -0800
Newsgroups gmane.comp.java.beanshell.devel
Message-ID <B1B25C5DF418644C8391261C189B58DA011850CF@SDOMSGMB00.corp.fairisaac.com>
hi bsh,

while doing some profiling on our application, i discovered that
JConsole leaks two threads per instance. these threads are created when
JConsole is constructed and last for the lifetime of the VM. i have a
fix, but it involves a behavioral change to JConsole that will break
current usages. i'd like to ask if anyone else has encountered this
problem and has an alternative solution, preferably one that doesn't
break things.

the behavioral change is threefold:
* JConsole no longer creates a thread in its constructor
* JConsole checks Thread.currentThread().isInterrupted() periodically
and terminates if true
* JConsole's i/o thread is interrupted when JConsole terminates

here is the difference in user code between old and new versions:

// old: console does its own threading
JConsole c = new JConsole();
// console is now running with no control
Interpreter i = new Interpreter(c);
i.run();


// new: user does console threading
JConsole c = new JConsole();
// one extra step: now user can control console's threading
Thread t = new Thread(c).start();
Interpreter i = new Interpreter(c);
i.run();


attached is a gz of my changed JConsole.java. please let me know if it's
acceptable into cvs or not, as we'll need to include this source in our
distribution if not (lgpl and all that). i guess i could also just add
an alternate constructor to JConsole with a flag to turn threading on or
off, but that leaves the thread leak in place where it could cause harm
to innocent passersby...

thanks!
john
JConsole.java.gz (application/x-gzip, 6.7 KB) - not displayed