有返回值的线程
作者:互联网
- 实现CallAble接口
- 实例:
@SuppressWarnings("unchecked")
public class CallAbleTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
CTest c1 = new CTest("a");
CTest c2 = new CTest("b");
ExecutorService e = Executors.newFixedThreadPool(2);
Future<String> f1 = e.submit(c1);
Future<String> f2 = e.submit(c2);
System.out.println(f1.get());
System.out.println(f2.get());
e.shutdown();
}
public static class CTest implements Callable{
private String name;
public CTest(String name) {
this.name = name;
}
public Object call() throws Exception {
return name+"返回值";
}
}
}
标签:f1,f2,String,线程,返回值,public,CTest,name 来源: https://www.cnblogs.com/kungFuPander/p/11711702.html