Public Methods | |
abstract Object | construct () |
Compute the value to be returned by the get method. More... | |
void | finished () |
Called on the event dispatching thread (not on the worker thread) after the construct method has returned. More... | |
void | interrupt () |
A new method that interrupts the worker thread. More... | |
Object | get () |
Return the value created by the construct method. More... | |
SwingWorker () | |
Start a thread that will call the construct method and then exit. More... | |
Protected Methods | |
synchronized Object | getValue () |
Get the value produced by the worker thread, or null if it hasn't been constructed yet. More... |
For instructions on using this class, see http://java.sun.com/products/jfc/swingdoc-current/threads2.html
|
Start a thread that will call the
00098 { 00099 final Runnable doFinished = new Runnable() { 00100 public void run() { finished(); } 00101 }; 00102 00103 Runnable doConstruct = new Runnable() { 00104 public void run() { 00105 try { 00106 //setValue(construct()); 00107 Object obj = construct(); 00108 setValue(obj); 00109 } 00110 finally { 00111 threadVar.clear(); 00112 } 00113 00114 SwingUtilities.invokeLater(doFinished); 00115 } 00116 }; 00117 00118 Thread t = new Thread(doConstruct); 00119 //t.setPriority(Thread.MAX_PRIORITY); 00120 threadVar = new ThreadVar(t); 00121 t.start(); 00122 } |
|
Get the value produced by the worker thread, or null if it hasn't been constructed yet.
00035 { 00036 return value; 00037 } |
|
Compute the value to be returned by the
|
|
Called on the event dispatching thread (not on the worker thread) after the
00055 { 00056 } |
|
A new method that interrupts the worker thread. Call this method to force the worker to abort what it's doing.
00062 { 00063 Thread t = threadVar.get(); 00064 if (t != null) { 00065 t.interrupt(); 00066 } 00067 threadVar.clear(); 00068 } |
|
Return the value created by the Returns null if either the constructing thread or the current thread was interrupted before a value was produced.
00077 { 00078 while (true) { 00079 Thread t = threadVar.get(); 00080 if (t == null) { 00081 return getValue(); 00082 } 00083 try { 00084 t.join(); 00085 } 00086 catch (InterruptedException e) { 00087 Thread.currentThread().interrupt(); // propagate 00088 return null; 00089 } 00090 } 00091 } |