SwingWorker.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: SwingWorker.java,v 1.2 2005/10/10 18:02:59 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.util;
  22. import javax.swing.SwingUtilities;
  23. /**
  24.  * This is the 3rd version of SwingWorker (also known as
  25.  * SwingWorker 3), an abstract class that you subclass to
  26.  * perform GUI-related work in a dedicated thread.  For
  27.  * instructions on using this class, see:
  28.  * 
  29.  * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
  30.  *
  31.  * Note that the API changed slightly in the 3rd version:
  32.  * You must now invoke start() on the SwingWorker after
  33.  * creating it.
  34.  */
  35. public abstract class SwingWorker {
  36.     private Object value;  // see getValue(), setValue()
  37.     private Thread thread;
  38.     /** 
  39.      * Class to maintain reference to current worker thread
  40.      * under separate synchronization control.
  41.      */
  42.     private static class ThreadVar {
  43.         private Thread thread;
  44.         ThreadVar(Thread t) { thread = t; }
  45.         synchronized Thread get() { return thread; }
  46.         synchronized void clear() { thread = null; }
  47.     }
  48.     private ThreadVar threadVar;
  49.     /** 
  50.      * Get the value produced by the worker thread, or null if it 
  51.      * hasn't been constructed yet.
  52.      */
  53.     protected synchronized Object getValue() { 
  54.         return value; 
  55.     }
  56.     /** 
  57.      * Set the value produced by worker thread 
  58.      */
  59.     private synchronized void setValue(Object x) { 
  60.         value = x; 
  61.     }
  62.     /** 
  63.      * Compute the value to be returned by the <code>get</code> method. 
  64.      */
  65.     public abstract Object construct();
  66.     /**
  67.      * Called on the event dispatching thread (not on the worker thread)
  68.      * after the <code>construct</code> method has returned.
  69.      */
  70.     public void finished() {
  71.     }
  72.     /**
  73.      * A new method that interrupts the worker thread.  Call this method
  74.      * to force the worker to stop what it's doing.
  75.      */
  76.     public void interrupt() {
  77.         Thread t = threadVar.get();
  78.         if (t != null) {
  79.             t.interrupt();
  80.         }
  81.         threadVar.clear();
  82.     }
  83.     /**
  84.      * Return the value created by the <code>construct</code> method.  
  85.      * Returns null if either the constructing thread or the current
  86.      * thread was interrupted before a value was produced.
  87.      * 
  88.      * @return the value created by the <code>construct</code> method
  89.      */
  90.     public Object get() {
  91.         while (true) {  
  92.             Thread t = threadVar.get();
  93.             if (t == null) {
  94.                 return getValue();
  95.             }
  96.             try {
  97.                 t.join();
  98.             }
  99.             catch (InterruptedException e) {
  100.                 Thread.currentThread().interrupt(); // propagate
  101.                 return null;
  102.             }
  103.         }
  104.     }
  105.     /**
  106.      * Start a thread that will call the <code>construct</code> method
  107.      * and then exit.
  108.      */
  109.     public SwingWorker() {
  110.         final Runnable doFinished = new Runnable() {
  111.            public void run() { finished(); }
  112.         };
  113.         Runnable doConstruct = new Runnable() { 
  114.             public void run() {
  115.                 try {
  116.                     setValue(construct());
  117.                 }
  118.                 finally {
  119.                     threadVar.clear();
  120.                 }
  121.                 SwingUtilities.invokeLater(doFinished);
  122.             }
  123.         };
  124.         Thread t = new Thread(doConstruct);
  125.         threadVar = new ThreadVar(t);
  126.     }
  127.     /**
  128.      * Start the worker thread.
  129.      */
  130.     public void start() {
  131.         Thread t = threadVar.get();
  132.         if (t != null) {
  133.             t.start();
  134.         }
  135.     }
  136. }