如果返回的值不是我关注的话,我应该如何在ExecutorService的 提交或执行之间进行选择?
如果我同时测试两者,除了返回值之外,我没有看到两者之间有任何差异.
ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
threadExecutor.execute(new Task());
Run Code Online (Sandbox Code Playgroud)
ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
threadExecutor.submit(new Task());
Run Code Online (Sandbox Code Playgroud) 我正在研究用于并行计算JavaSeis.org的软件开发框架.我需要一个强大的机制来报告线程异常.在开发过程中,了解异常的来源具有很高的价值,因此我想在过度报告方面犯错误.我也希望能够在线程中处理Junit4测试.方法是否合理或有更好的方法吗?
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestThreadFailure {
public static void main(String[] args) {
int size = 1;
ExecutorService exec = Executors.newFixedThreadPool(size);
ThreadFailTask worker = new ThreadFailTask();
Future<Integer> result = exec.submit(worker);
try {
Integer value = result.get();
System.out.println("Result: " + value);
} catch (Throwable t) {
System.out.println("Caught failure: " + t.toString());
exec.shutdownNow();
System.out.println("Stack Trace:");
t.printStackTrace();
return;
}
throw new RuntimeException("Did not catch failure !!");
}
public static class ThreadFailTask implements Callable<Integer> {
@Override …Run Code Online (Sandbox Code Playgroud)