1 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 ; 10 import java.util.List ; 11 12 13 public class ActiveBean implements InitializingBean, DisposableBean, 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 public List 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 { 35 this.t = new Thread (this, "ActiveBean for "+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); } catch (InterruptedException e) { 66 } 68 } 69 } 70 } 71 72 } 73 74 | Popular Tags |