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: public class BeanWithAutolock implements IActiveBean, Runnable {
07: private String value = "0";
08: private Object mutex = new Object();
09:
10: private transient boolean started;
11: private transient boolean active;
12: private transient String localValue;
13:
14: public String getValue() {
15: synchronized (this ) {
16: return this .value;
17: }
18: }
19:
20: public void setValue(String value) {
21: this .localValue = value;
22: }
23:
24: public boolean isActive() {
25: return active;
26: }
27:
28: public void start() {
29: this .started = true;
30:
31: Thread thread = new Thread(this );
32: thread.start();
33: }
34:
35: public void stop() {
36: this .started = false;
37: }
38:
39: public void run() {
40: try {
41: synchronized (mutex) {
42: while (started) {
43: this .active = true;
44: if (this .localValue != null) {
45: this .value = this .localValue;
46: this .localValue = null;
47: }
48:
49: try {
50: Thread.sleep(100L);
51: } catch (InterruptedException e) {
52: // ignore
53: }
54: }
55: }
56: } finally {
57: this .active = false;
58: }
59: }
60:
61: }
|