1肺孵、接收的參數(shù)不一樣
public class MainTest {
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(10);
pool.execute(new Runnable() {
@Override
public void run() {
System.out.println("execute");
}
});
Future<?> submit = pool.submit(new Runnable() {
@Override
public void run() {
System.out.println("submit");
}
});
//等任務(wù)執(zhí)行完畢會打印null
System.out.println(submit.get());
FutureTask<Integer> submit2 = (FutureTask<Integer>) pool.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println("submit_2");
return 2;
}
});
System.out.println("result=" + submit2.get());
}
}
2傻挂、返回值不一樣
submit有返回值Future叹坦,而execute沒有害淤。
Java FutureTask
3扇雕、submit方便Exception處理
public class MainTest {
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(10);
Future submit = pool.submit(new Runnable() {
@Override
public void run() {
System.out.println("submit");
System.out.println(0/0);
}
});
try {
System.out.println("result=" + submit.get());
}catch (Exception e){
System.out.println(e);
}
}
}