KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > adminGui > util > ThreadWorker


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: ThreadWorker.java,v 1.1 2003/03/07 13:49:45 per_nyfelt Exp $
8
package org.ozoneDB.adminGui.util;
9
10 import javax.swing.*;
11
12 /**
13  * ThreadWorker, an abstract class that you subclass to
14  * perform GUI-related work in a dedicated thread.
15  *
16  */

17 public abstract class ThreadWorker {
18     private Object JavaDoc value;
19     private ThreadVar threadVar;
20
21     /**
22      * Class to maintain reference to current worker thread
23      * under separate synchronization control.
24      */

25     private static class ThreadVar {
26         private Thread JavaDoc thread;
27
28         ThreadVar(Thread JavaDoc t) {
29             thread = t;
30         }
31
32         synchronized Thread JavaDoc get() {
33             return thread;
34         }
35
36         synchronized void clear() {
37             thread = null;
38         }
39     }
40
41     /**
42      * Get the value produced by the worker thread, or null if it
43      * hasn't been constructed yet.
44      */

45     protected synchronized Object JavaDoc getValue() {
46         return value;
47     }
48
49     /**
50      * Set the value produced by worker thread
51      */

52     private synchronized void setValue(Object JavaDoc x) {
53         value = x;
54     }
55
56     /**
57      * Compute the value to be returned by the <code>get</code> method.
58      */

59     public abstract Object JavaDoc construct();
60
61     /**
62      * Called on the event dispatching thread (not on the worker thread)
63      * after the <code>construct</code> method has returned.
64      */

65     public void finished() {
66     }
67
68     /**
69      * A new method that interrupts the worker thread. Call this method
70      * to force the worker to stop what it's doing.
71      */

72     public void interrupt() {
73         Thread JavaDoc t = threadVar.get();
74         if (t != null) {
75             t.interrupt();
76         }
77         threadVar.clear();
78     }
79
80     /**
81      * Return the value created by the <code>construct</code> method.
82      * Returns null if either the constructing thread or the current
83      * thread was interrupted before a value was produced.
84      *
85      * @return the value created by the <code>construct</code> method
86      */

87     public Object JavaDoc get() {
88         while (true) {
89             Thread JavaDoc t = threadVar.get();
90             if (t == null) {
91                 return getValue();
92             }
93             try {
94                 t.join();
95             } catch (InterruptedException JavaDoc e) {
96                 Thread.currentThread().interrupt(); // propagate
97
return null;
98             }
99         }
100     }
101
102
103     /**
104      * Start a thread that will call the <code>construct</code> method
105      * and then exit.
106      */

107     public ThreadWorker() {
108         final Runnable JavaDoc doFinished = new Runnable JavaDoc() {
109             public void run() {
110                 finished();
111             }
112         };
113
114         Runnable JavaDoc doConstruct = new Runnable JavaDoc() {
115             public void run() {
116                 try {
117                     setValue(construct());
118                 } finally {
119                     threadVar.clear();
120                 }
121
122                 SwingUtilities.invokeLater(doFinished);
123             }
124         };
125
126         Thread JavaDoc t = new Thread JavaDoc(doConstruct);
127         threadVar = new ThreadVar(t);
128     }
129
130     /**
131      * Start the worker thread.
132      */

133     public void start() {
134         Thread JavaDoc t = threadVar.get();
135         if (t != null) {
136             t.start();
137         }
138     }
139 }
140
Popular Tags