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

📄 backgroundtask.java

📁 java concurrency in practice 源码. JAVA并发设计
💻 JAVA
字号:
package net.jcip.examples;import java.util.concurrent.*;/** * BackgroundTask * <p/> * Background task class supporting cancellation, completion notification, and progress notification * * @author Brian Goetz and Tim Peierls */public abstract class BackgroundTask <V> implements Runnable, Future<V> {    private final FutureTask<V> computation = new Computation();    private class Computation extends FutureTask<V> {        public Computation() {            super(new Callable<V>() {                public V call() throws Exception {                    return BackgroundTask.this.compute();                }            });        }        protected final void done() {            GuiExecutor.instance().execute(new Runnable() {                public void run() {                    V value = null;                    Throwable thrown = null;                    boolean cancelled = false;                    try {                        value = get();                    } catch (ExecutionException e) {                        thrown = e.getCause();                    } catch (CancellationException e) {                        cancelled = true;                    } catch (InterruptedException consumed) {                    } finally {                        onCompletion(value, thrown, cancelled);                    }                };            });        }    }    protected void setProgress(final int current, final int max) {        GuiExecutor.instance().execute(new Runnable() {            public void run() {                onProgress(current, max);            }        });    }    // Called in the background thread    protected abstract V compute() throws Exception;    // Called in the event thread    protected void onCompletion(V result, Throwable exception,                                boolean cancelled) {    }    protected void onProgress(int current, int max) {    }    // Other Future methods just forwarded to computation    public boolean cancel(boolean mayInterruptIfRunning) {        return computation.cancel(mayInterruptIfRunning);    }    public V get() throws InterruptedException, ExecutionException {        return computation.get();    }    public V get(long timeout, TimeUnit unit)            throws InterruptedException,            ExecutionException,            TimeoutException {        return computation.get(timeout, unit);    }    public boolean isCancelled() {        return computation.isCancelled();    }    public boolean isDone() {        return computation.isDone();    }    public void run() {        computation.run();    }}

⌨️ 快捷键说明

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