线程池ThreadPoolExecutor的submit方法
ThreadPool
A simple C++11 Thread Pool implementation
项目地址:https://gitcode.com/gh_mirrors/th/ThreadPool
免费下载资源
·
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
结论:终于还是没搞懂,这两个函数的使用场景是什么。
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
三个重载方法,第一个很好理解,传入callable,本身call函数就有一个返回值,可以正常取得。
第二个第三个就不清楚怎么可以取到返回值了,因为Runnable本身的run方法也没有返回值,为什么要这么做,如果没有返回值,直接用execute方法就好,为什么还要下面这几个submit方法呢。
package thread.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class PoolTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<?> submit = executor.submit(new Runnable(){
@Override
public void run() {
System.out.println("hehe");
}
});
try {
System.out.println(submit.get());
} catch (Exception e) {
}
}
}
输出:
hehe
null
submit提交的任务,正常执行,返回的是null,不清楚有什么意义
package thread.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class PoolTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<?> submit = executor.submit(new Runnable(){
@Override
public void run() {
System.out.println("how");
throw new RuntimeException("hehe");
}
});
try {
Object obj = submit.get();
System.out.println(obj);
} catch (Exception e) {
System.out.println(e.getMessage() + " exception");
}
}
}
how
java.lang.RuntimeException: hehe exception
任务里抛出异常,返回值可以get到该异常,继续抛出,不懂。
package thread.pool;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
public class PoolTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
FutureTask<String> ft = new FutureTask<String>(new Callable<String>(){
@Override
public String call() throws Exception {
return "hehe";
}
});
Future<?> submit = executor.submit(ft);
try {
;
System.out.println("1" + submit.get());
System.out.println("2" + ft.get());
} catch (Exception e) {
System.out.println(e.getMessage() + " exception");
}
executor.shutdown();
}
}
输出:
1null
2hehe
package thread.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class PoolTest {
static String aa = "he";
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
Future<?> submit = executor.submit(new Runnable(){
@Override
public void run() {
System.out.println("how");
aa = aa + "he";
}
},aa);
try {
System.out.println(submit.get());
} catch (Exception e) {
}
}
}
输出:
how
he
结论:终于还是没搞懂,这两个函数的使用场景是什么。
觉得应该是:
execute提交runnable执行就好
submit提交callable,取得返回值就好
为什么要submit提交runnable呢
GitHub 加速计划 / th / ThreadPool
7.74 K
2.22 K
下载
A simple C++11 Thread Pool implementation
最近提交(Master分支:2 个月前 )
9a42ec13 - 9 年前
fcc91415 - 9 年前
更多推荐
已为社区贡献1条内容
所有评论(0)