【发布时间】:2019-02-19 09:30:17
【问题描述】:
我想通过 callable 和 future 从服务中获取一些数据。这是我的代码之一:
@Override
public void getCIFilesType(Consumer<String> consumer) {
try {
consumer.accept(serviceExecutor.submit(() ->
service.getCi(EsupFactory.getConfigString(SETTING_ROOT_CI) + "GetCI",
translator.makeCiJsonObject("PCiName", "CI_FilesType")).execute())
.get().body().string());
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我有 10 个像上面这样执行的方法。我使用 Executor 服务来运行 callable :
ExecutorService serviceExecutor = Executors.newSingleThreadExecutor();
我是我的活动我有一个菜单,然后单击菜单中的一项,片段是活动中的事务。所有线程任务立即在片段中的onViewCreated 开始:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
presenter.getCis();
}
但是当我点击菜单项时,UI 被卷曲,直到所有任务都停止,然后事务就停止了。这不是我第一次遇到这个问题。每次我使用 callable 和 executor 服务时,我都不知道为什么 UI 是毛茸茸的!!!!
这是分析器:
有人给我一些指导!!?请不要告诉我使用 asyncTask :-)
什么是读行??在ui线程中我只是做事务而不执行长时间运行的任务!!!
【问题讨论】: