KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > engine > DatabaseCloser


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.engine;
6
7 import java.lang.ref.WeakReference JavaDoc;
8
9 public class DatabaseCloser extends Thread JavaDoc {
10     private WeakReference JavaDoc databaseRef;
11     private int delayInMillis;
12     private boolean shutdownHook;
13     
14     DatabaseCloser(Database db, int delayInMillis, boolean shutdownHook) {
15         this.databaseRef = new WeakReference JavaDoc(db);
16         this.delayInMillis = delayInMillis;
17         this.shutdownHook = shutdownHook;
18     }
19     
20     public void reset() {
21         synchronized(this) {
22             databaseRef = null;
23         }
24     }
25     
26     public void run() {
27         while(delayInMillis > 0) {
28             try {
29                 int step = 100;
30                 Thread.sleep(step);
31                 delayInMillis -= step;
32             } catch(Exception JavaDoc e) {
33                 // ignore
34
}
35             if(databaseRef == null) {
36                 return;
37             }
38         }
39         synchronized(this) {
40             if(databaseRef != null) {
41                 Database database = (Database) databaseRef.get();
42                 if(database != null) {
43                     database.close(shutdownHook);
44                 }
45             }
46         }
47     }
48     
49 }
50
Popular Tags