Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Terminating a thread when program exits.

Reply
Thread Tools

Terminating a thread when program exits.

 
 
Ian Wilson
Guest
Posts: n/a
 
      01-23-2007
Sun's developer online training website has an example of a JDBC
connection pool.
http://java.sun.com/developer/online...k/conpool.html

One of the things it does is start a "reaper" thread that watches for
stale connections (see below).

If I run my test program from Eclipse, this reaper thread keeps running
when my program has otherwise finished. I didn't notice this to start
with and so after a few test runs Eclipse started reporting that the VM
was out of memory.

I think I need to replace "while(true") below with "while(poolExists)"
and set poolExists to false somehow when my program wants to exit.

The JDCConnectionPool is instantiated by the constructor of
JDCConnectionDriver which I instantiate in my application start-up.

Because the pool is written as a JDBC driver wrapper, there's not much
leverage for the app to force a clean up.

I suppose I could add a finalize() to JDCConnectionDriver and some
methods in JDBCConnectionPool and ConnectionReaper to set poolExists false.

I'm not sure if this is a good way to go about this, ideas?

Also the sleep() lasts 5 mins, I'd prefer some way to force a quicker
interruption of this sleep() - Thread.interrupt()?

---------------- from JDCConnectionPool -----------------------
class ConnectionReaper extends Thread {

private JDCConnectionPool pool;
private final long delay=300000;

ConnectionReaper(JDCConnectionPool pool) {
this.pool=pool;
}

public void run() {
while(true) {
try {
sleep(delay);
} catch( InterruptedException e) { }
pool.reapConnections();
}
}
}
-----------------------------------------
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      01-23-2007
Ian Wilson wrote:
[...]
> If I run my test program from Eclipse, this reaper thread keeps running
> when my program has otherwise finished.

[...]
> class ConnectionReaper extends Thread {
>
> private JDCConnectionPool pool;
> private final long delay=300000;
>
> ConnectionReaper(JDCConnectionPool pool) {
> this.pool=pool;

this.setDaemon(true); // see API doc of Thread#setDaemon
> }
>
> public void run() {
> while(true) {
> try {
> sleep(delay);
> } catch( InterruptedException e) { }
> pool.reapConnections();
> }
> }
> }



--
Thomas
 
Reply With Quote
 
 
 
 
vjg
Guest
Posts: n/a
 
      01-23-2007

Ian Wilson wrote:
> Sun's developer online training website has an example of a JDBC
> connection pool.
> http://java.sun.com/developer/online...k/conpool.html
>
> One of the things it does is start a "reaper" thread that watches for
> stale connections (see below).
>
> If I run my test program from Eclipse, this reaper thread keeps running
> when my program has otherwise finished. I didn't notice this to start
> with and so after a few test runs Eclipse started reporting that the VM
> was out of memory.
>
> I think I need to replace "while(true") below with "while(poolExists)"
> and set poolExists to false somehow when my program wants to exit.
>
> The JDCConnectionPool is instantiated by the constructor of
> JDCConnectionDriver which I instantiate in my application start-up.
>
> Because the pool is written as a JDBC driver wrapper, there's not much
> leverage for the app to force a clean up.
>
> I suppose I could add a finalize() to JDCConnectionDriver and some
> methods in JDBCConnectionPool and ConnectionReaper to set poolExists false.
>
> I'm not sure if this is a good way to go about this, ideas?


I'd just set the reaper thread to be a daemon thread before starting
it. That way it will die a natural death when the JVM shuts down (i.e.
when your application closes down normally).

> Also the sleep() lasts 5 mins, I'd prefer some way to force a quicker
> interruption of this sleep() - Thread.interrupt()?
>
> ---------------- from JDCConnectionPool -----------------------
> class ConnectionReaper extends Thread {
>
> private JDCConnectionPool pool;
> private final long delay=300000;
>
> ConnectionReaper(JDCConnectionPool pool) {
> this.pool=pool;
> }
>
> public void run() {
> while(true) {
> try {
> sleep(delay);
> } catch( InterruptedException e) { }
> pool.reapConnections();
> }
> }
> }
> -----------------------------------------


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-23-2007

Ian Wilson wrote:
> Sun's developer online training website has an example of a JDBC
> connection pool.
> http://java.sun.com/developer/online...k/conpool.html
>
> One of the things it does is start a "reaper" thread that watches for
> stale connections (see below).
>
> If I run my test program from Eclipse, this reaper thread keeps running
> when my program has otherwise finished. I didn't notice this to start
> with and so after a few test runs Eclipse started reporting that the VM
> was out of memory.
>

Once your application has terminated, no threads should be left
running.
You might have to ask Eclipse to forcefully terminate the child
process. If the process has completed, but your problem persists, it
would seem to be a bug with Eclipse, rather than your code.

All threads should terminate when Systen.exit() is called. Otherwise,
if only Daemon threads (see Thread.setDaemon) are active, the JVM
should quit.

 
Reply With Quote
 
Ian Wilson
Guest
Posts: n/a
 
      01-24-2007
Ian Wilson wrote:
> Sun's developer online training website has an example of a JDBC
> connection pool.
> http://java.sun.com/developer/online...k/conpool.html
>
> One of the things it does is start a "reaper" thread that watches for
> stale connections (see below).
>
> If I run my test program from Eclipse, this reaper thread keeps running
> when my program has otherwise finished.


Adding setDaemon(true) to the thread's constructor had the desired
effect. So simple!

Thanks Thomas, vjg, Daniel.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating program with "exit( )" ern C Programming 19 03-24-2006 07:47 PM
Terminating a thread from beanshell Uppu Java 0 02-27-2006 09:23 AM
Terminating a thread from the parent DE Python 3 05-24-2005 01:01 PM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Terminating program run from thread (not just the thread) Jeffrey Barish Python 0 05-28-2004 02:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57