After diving a while with the Alarm and FIFONotifier classes in the sandobx-threading project I found two bugs.
In the first the Alarm needs to synchronize itself to wait
In the second FIFONotifier doesn't starts itself. Also added a safety check in the start method
Index: Alarm.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/threading/src/java/org/apache/commons/threading/Alarm.java,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 Alarm.java
--- Alarm.java 2001/04/15 00:50:57 1.1.1.1
+++ Alarm.java 2001/05/10 15:55:28
@@ -478,8 +478,8 @@
thread.start();
}
-
+
}
@@ -513,6 +513,7 @@
*/
public void run() {
+
// Record starting time and fire the STARTED event
started = System.currentTimeMillis();
publisher.send(new AlarmEvent(this, STARTED,
@@ -521,7 +522,9 @@
// Loop for each wait for the timer to expire
while (true) {
try {
- wait(timeout);
+ synchronized (this) {
+ wait(timeout);
+ }
publisher.send(new AlarmEvent(this, EXPIRED,
System.currentTimeMillis()));
if (mode != CONTINUOUS) {
Index: FIFONotifier.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/threading/src/java/org/apache/commons/threading/FIFONotifier.java,v
retrieving revision 1.2
diff -u -r1.2 FIFONotifier.java
--- FIFONotifier.java 2001/05/06 16:50:22 1.2
+++ FIFONotifier.java 2001/05/10 15:53:07
@@ -134,9 +134,9 @@
public FIFONotifier(ThreadGroup group, String name) {
if (group == null) {
- thread = new Thread(name);
+ thread = new Thread(this, name);
} else {
- thread = new Thread(group, name);
+ thread = new Thread(group, this, name);
}
thread.setDaemon(true);
thread.start();
@@ -211,7 +211,9 @@
*/
public void start() {
+ if (thread.isAlive()) {
thread.start();
+ }
}
