java - 为什么newsinglethreadexcutor 会阻塞currentthread?
问题描述
我试图理解为什么newsinglethreadexecutor
使用可调用期货的get()
方法会阻止binderthread
其初始化的方法。我怎样才能让它们一起运行?
public class main {
public static void main(String[] args) {
final process prc = new process();
prc.run();
System.out.println("main thread" + Thread.currentThread().getName());
}
};
,,
import java.util.concurrent.*;
public class process {
ExecutorService es = Executors.newSingleThreadExecutor();
public void run() {
Future fut = es.submit(new Callable<String>() {
public String call() throws Exception {
for (byte b : new byte[]{1,2,3,4,4}){
System.out.println(b+Thread.currentThread().getName());
Thread.sleep(2000);
}
return "aa";
}
});
try {
// System.out.println(fut.get(1900L, TimeUnit.MILLISECONDS));
System.out.println(fut.get());
}catch (Exception a){}
}
}
解决方案
推荐阅读
- python - 如何删除lxml中的标签?
- javascript - 使用具有相同功能(如按键)的正则表达式验证 javascript 中的电话号码
- go - 获取 golang 的协议缓冲区选项信息
- c++ - Mfast 解码器.解码失败
- android - LinearLayout 无法在某些设备上添加视图
- .net - 如何使用 .net 中的 ping 类 ping redis 缓存?
- pandas - 如何在没有嵌套 for 循环的情况下遍历数据框?
- android - 用户注销时不显示通知
- android - 如何将.so 链接到 Visual Studio ndk 动态共享库
- linux - docker journald 日志驱动程序也会导致大量日志记录到 /var/log/syslog 和 /var/log/kern.log