⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingworker.java

📁 java图形用户界面 6.1 HelloWorldSwing 6.2 按钮计数器 6.3 Swing按钮 6.4 温度转换器 6.5 HtmlDemo 6.6 投票器 6.7 TextS
💻 JAVA
字号:
import javax.swing.SwingUtilities;public abstract class SwingWorker {    private Object value;      private Thread thread;    private static class ThreadVar {        private Thread thread;        ThreadVar(Thread t) { thread = t; }        synchronized Thread get() { return thread; }        synchronized void clear() { thread = null; }    }    private ThreadVar threadVar;    /**      * 获取worker线程产生的值,如果没有被构造,那么返回null      */    protected synchronized Object getValue() {         return value;     }    /**      * 设置由worker线程产生值      */    private synchronized void setValue(Object x) {         value = x;     }    /**      * 计算将要被<code>get</code>方法返回的值      */    public abstract Object construct();    /**     * 当<code>construct</code>方法返回后,调用事件线程     */    public void finished() {    }    /**     * 一个打扰worker线程的方法.调用该方法可以使worker线程停止工作     */    public void interrupt() {        Thread t = threadVar.get();        if (t != null) {            t.interrupt();        }        threadVar.clear();    }    /**     * 返回由<code>construct</code>方法创建的值,如果在该值产生之前     * 构造线程或者当前的线程都没有被打扰,那么返回null     */    public Object get() {        while (true) {              Thread t = threadVar.get();            if (t == null) {                return getValue();            }            try {                t.join();            }            catch (InterruptedException e) {                Thread.currentThread().interrupt();                 return null;            }        }    }    /**     * 启动线程将调用<code>construct</code>方法,然后退出     */    public SwingWorker() {        final Runnable doFinished = new Runnable() {           public void run() { finished(); }        };        Runnable doConstruct = new Runnable() {             public void run() {                try {                    setValue(construct());                }                finally {                    threadVar.clear();                }                SwingUtilities.invokeLater(doFinished);            }        };        Thread t = new Thread(doConstruct);        threadVar = new ThreadVar(t);    }    /**     * 开启worker线程     */    public void start() {        Thread t = threadVar.get();        if (t != null) {            t.start();        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -