ExecutorService executor = Executors.newSingleThreadExecutor(); // 使用Callable接口做爲構造參數 FutureTask<String> future = new FutureTask<String>(new Callable<String>() { public String call() throws InterruptedException { // 真正的任務在這裏執行,這裏的返回值類型爲String,能夠爲任意類型 return "call 返回數值!!"; } }); executor.execute(future); // 在這裏能夠作別的任何事情 try { String result = future.get(2000, TimeUnit.MILLISECONDS); System.out.println(result); } catch (InterruptedException e) { future.cancel(true); } catch (ExecutionException e) { future.cancel(true); } catch (TimeoutException e) { future.cancel(true); } finally { executor.shutdown(); }