KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class BeanWithAutolock implements IActiveBean, Runnable JavaDoc {
7   private String JavaDoc value = "0";
8   private Object JavaDoc mutex = new Object JavaDoc();
9
10   private transient boolean started;
11   private transient boolean active;
12   private transient String JavaDoc localValue;
13
14   public String JavaDoc getValue() {
15     synchronized (this) {
16       return this.value;
17     }
18   }
19
20   public void setValue(String JavaDoc 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 JavaDoc thread = new Thread JavaDoc(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 JavaDoc e) {
52             // ignore
53
}
54         }
55       }
56     } finally {
57       this.active = false;
58     }
59   }
60
61 }
62
Popular Tags