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; } }
理解Java的Future模式(3)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://www.heiqu.com/6c3f6144697a61d0788362bf68d0e50b.html