KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > JoinableTask


1 package org.lobobrowser.util;
2
3 /**
4  * A task that can be used in a thread or thread pool.
5  * The caller can wait for the task to finish by joining it.
6  */

7 public abstract class JoinableTask implements SimpleThreadPoolTask {
8     private boolean done = false;
9     
10     public final void run() {
11         try {
12             this.execute();
13         } finally {
14             synchronized(this) {
15                 this.done = true;
16                 this.notifyAll();
17             }
18         }
19     }
20
21     public final void forceDone() {
22         synchronized(this) {
23             this.done = true;
24             this.notifyAll();
25         }
26     }
27     
28     public void join() throws InterruptedException JavaDoc {
29         synchronized(this) {
30             while(!this.done) {
31                 this.wait();
32             }
33         }
34     }
35     
36     public void cancel() {
37         this.forceDone();
38     }
39     
40     protected abstract void execute();
41 }
42
Popular Tags