KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > bean > ActiveBean


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.spring.bean;
5
6 import org.springframework.beans.factory.DisposableBean;
7 import org.springframework.beans.factory.InitializingBean;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12
13 public class ActiveBean implements InitializingBean, DisposableBean, Runnable JavaDoc {
14
15   private transient Thread JavaDoc t;
16   private transient boolean stopped;
17   private transient boolean[] running = new boolean[] { false };
18
19   private List JavaDoc instances = new ArrayList JavaDoc();
20   private final Object JavaDoc lock = new Object JavaDoc();
21   
22   //
23
public List JavaDoc getInstances() {
24     synchronized (instances) {
25       return instances;
26     }
27   }
28   
29   public boolean isStopped() {
30     return stopped;
31   }
32   
33
34     public void afterPropertiesSet() throws Exception JavaDoc {
35         this.t = new Thread JavaDoc(this, "ActiveBean for "+Thread.currentThread().getName());
36         this.t.start();
37     }
38   
39   public void destroy() throws Exception JavaDoc {
40     stopped = true;
41
42     synchronized(running) {
43       while (!running[0]) {
44         running.wait();
45       }
46     }
47     
48     synchronized(instances) {
49       instances.remove(0);
50     }
51   }
52   
53   public void run() {
54     synchronized (this.lock) {
55       synchronized(instances) {
56         instances.add(Thread.currentThread().getName());
57         synchronized(running) {
58           running[0] = true;
59           running.notifyAll();
60         }
61       }
62       while (!stopped) {
63         try {
64           Thread.sleep(100L); // TODO use wait/notifyAll instead
65
} catch (InterruptedException JavaDoc e) {
66           // ignore
67
}
68       }
69     }
70   }
71   
72 }
73
74
Popular Tags