preloader.java

来自「java concurrency in practice 源码. JAVA」· Java 代码 · 共 46 行

JAVA
46
字号
package net.jcip.examples;import java.util.concurrent.*;/** * Preloader * * Using FutureTask to preload data that is needed later * * @author Brian Goetz and Tim Peierls */public class Preloader {    ProductInfo loadProductInfo() throws DataLoadException {        return null;    }    private final FutureTask<ProductInfo> future =        new FutureTask<ProductInfo>(new Callable<ProductInfo>() {            public ProductInfo call() throws DataLoadException {                return loadProductInfo();            }        });    private final Thread thread = new Thread(future);    public void start() { thread.start(); }    public ProductInfo get()            throws DataLoadException, InterruptedException {        try {            return future.get();        } catch (ExecutionException e) {            Throwable cause = e.getCause();            if (cause instanceof DataLoadException)                throw (DataLoadException) cause;            else                throw LaunderThrowable.launderThrowable(cause);        }    }    interface ProductInfo {    }}class DataLoadException extends Exception { }

⌨️ 快捷键说明

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