import java.util.concurrent.Callable;
public class MyCollable implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("我被执行了");
return "执行完毕";
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Collable实现带返回值多线程
*/
public class Deom {
public static void main(String[] args) {
try {
FutureTask<String> stringFutureTask = new FutureTask<>(new MyCollable());
new Thread(stringFutureTask).start();
String s = stringFutureTask.get();
System.out.println(s);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Callable实现带返回值多线程
最新推荐文章于 2022-08-16 08:00:01 发布