领略Java的Future模式(3)

public class GetTime { public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException { ExecutorService executor= Executors.newFixedThreadPool(2); //建设一个Callable,三秒返回String范例 Callable callable = new Callable() { @Override public Object call() throws Exception { Thread.sleep(3000); System.out.println("callable要领执行!"); return "callable"; } }; System.out.println("提交任务之前:"+getStringDate()); Future future = executor.submit(callable); System.out.println("提供任务之后,获取功效之前:"+getStringDate()); System.out.println("获取返回值:"+future.get(2,TimeUnit.SECONDS)); System.out.println("获取到功效之后:"+getStringDate()); } public static String getStringDate(){ Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); String dataString = format.format(date); return dataString; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsdjfs.html