KickJava   Java API By Example, From Geeks To Geeks.

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


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 BeanWithWaitNotify implements IActiveBean, Runnable JavaDoc {
7   private int value = 0;
8   private Object JavaDoc mutex = new Object JavaDoc();
9
10   private transient boolean started;
11
12   public String JavaDoc getValue() {
13     synchronized (this) {
14       return "" + value;
15     }
16   }
17
18   public void setValue(String JavaDoc 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 JavaDoc thread = new Thread JavaDoc(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 JavaDoc e) {
49           //
50
}
51       }
52     }
53   }
54
55 }
56
Popular Tags