1 4 package com.tctest.spring.bean; 5 6 public class BeanWithWaitNotify implements IActiveBean, Runnable { 7 private int value = 0; 8 private Object mutex = new Object (); 9 10 private transient boolean started; 11 12 public String getValue() { 13 synchronized (this) { 14 return "" + value; 15 } 16 } 17 18 public void setValue(String value) { 19 synchronized (mutex) { 20 mutex.notifyAll(); 21 } 22 } 23 24 public boolean isActive() { 25 return started; 26 } 27 28 public void start() { 29 started = true; 30 31 Thread thread = new Thread (this); 32 thread.start(); 33 } 34 35 public void stop() { 36 started = false; 37 synchronized (mutex) { 38 mutex.notifyAll(); 39 } 40 } 41 42 public void run() { 43 while (started) { 44 synchronized (mutex) { 45 try { 46 mutex.wait(); 47 value++; 48 } catch (InterruptedException e) { 49 } 51 } 52 } 53 } 54 55 } 56 | Popular Tags |