Terminating a thread when program exits.

Discussion in 'Java' started by Ian Wilson, Jan 23, 2007.

  1. Ian Wilson

    Ian Wilson Guest

    Sun's developer online training website has an example of a JDBC
    connection pool.
    http://java.sun.com/developer/onlineTraining/Programming/JDCBook/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();
    }
    }
    }
    -----------------------------------------
     
    Ian Wilson, Jan 23, 2007
    #1
    1. Advertising

  2. 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
     
    Thomas Fritsch, Jan 23, 2007
    #2
    1. Advertising

  3. Ian Wilson

    vjg Guest

    Ian Wilson wrote:
    > Sun's developer online training website has an example of a JDBC
    > connection pool.
    > http://java.sun.com/developer/onlineTraining/Programming/JDCBook/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();
    > }
    > }
    > }
    > -----------------------------------------
     
    vjg, Jan 23, 2007
    #3
  4. Ian Wilson

    Daniel Pitts Guest

    Ian Wilson wrote:
    > Sun's developer online training website has an example of a JDBC
    > connection pool.
    > http://java.sun.com/developer/onlineTraining/Programming/JDCBook/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.
     
    Daniel Pitts, Jan 23, 2007
    #4
  5. Ian Wilson

    Ian Wilson Guest

    Ian Wilson wrote:
    > Sun's developer online training website has an example of a JDBC
    > connection pool.
    > http://java.sun.com/developer/onlineTraining/Programming/JDCBook/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.
     
    Ian Wilson, Jan 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,585
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. Uppu
    Replies:
    0
    Views:
    368
  3. Jeffrey Barish
    Replies:
    0
    Views:
    318
    Jeffrey Barish
    May 28, 2004
  4. DE
    Replies:
    3
    Views:
    403
  5. Giampaolo Rodola'

    Call a function when a thread exits

    Giampaolo Rodola', May 8, 2009, in forum: Python
    Replies:
    2
    Views:
    275
    Giampaolo Rodola'
    May 8, 2009
Loading...

Share This Page