示例:
package Multithreading;
import java.util.concurrent.*;
public class TestCallable implements Callable {
@Override
public Object call() throws Exception {
return "Callable";
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable callable = new TestCallable();
// 创建服务执行
ExecutorService service = Executors.newFixedThreadPool(1);
// 提交执行
Future<String> s1 = service.submit(callable);
// 获取结果
String str01 = s1.get();
// 关闭服务
service.shutdown();
System.out.println(str01);
}
}
运行结果: