01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest.spring.bean;
05:
06: import org.springframework.beans.factory.DisposableBean;
07: import org.springframework.beans.factory.InitializingBean;
08:
09: import java.util.ArrayList;
10: import java.util.List;
11:
12: public class ActiveBean implements InitializingBean, DisposableBean,
13: Runnable {
14:
15: private transient Thread t;
16: private transient boolean stopped;
17: private transient boolean[] running = new boolean[] { false };
18:
19: private List instances = new ArrayList();
20: private final Object lock = new Object();
21:
22: //
23: public List getInstances() {
24: synchronized (instances) {
25: return instances;
26: }
27: }
28:
29: public boolean isStopped() {
30: return stopped;
31: }
32:
33: public void afterPropertiesSet() throws Exception {
34: this .t = new Thread(this , "ActiveBean for "
35: + Thread.currentThread().getName());
36: this .t.start();
37: }
38:
39: public void destroy() throws Exception {
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 e) {
66: // ignore
67: }
68: }
69: }
70: }
71:
72: }
|