Future 接口:
示例:
package com.github.thread2.demo5;
import java.util.concurrent.Callable;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Call implements Callable<String> {
@Override
public String call() throws Exception {
return "你好,世界";
}
}
package com.github.thread2.demo5;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* @author maxiaoke.com
* @version 1.0
*
*/
public class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 创建线程池
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future<String> submit = executorService.submit(new Call());
String s = submit.get();
System.out.println(s);
// 关闭线程池
executorService.shutdown();
}
}