0

我有一个方法。在这个方法中,我运行 Executors 的任务。它的工作是从服务器获取数据并填充结果数组列表。

   public Paginator(String secSrv, int total, ExecutorService executorService, Cookie cookie) {
    securitySrv = secSrv;
    totalItems = total;
    this.executorService = executorService;
    this.cookie = cookie;
}

ArrayList<Suser> results = new ArrayList<>();

public ArrayList<Suser> generatePage(int currentPage) {

    fetchAllUsers(currentPage);
    ......
    for (int i = startItem; i < startItem + numOfData; i++) {
            pageData.add(results.get(i-1));
        }

填充结果 Arraylist 后,我​​必须使用这个 arraylist。我怎么知道 executorService 在哪里完成了它的工作,我可以再次继续我的方法?

   private void fetchAllUsers(final int currentPage) {

    Runnable fetchUserListFromSrv = new Runnable() {
        @Override
        public void run() {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
            JSONObject count = new JSONObject();
            try {
                count.put("count", 10);
                count.put("page", currentPage);
                String SearchUsersPagingResult = ...
                results.addAll(susers) ;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    executorService.submit(fetchUserListFromSrv);
}
4

1 回答 1

1

使用文档。所有答案都在这里。

未来| 安卓开发者

执行服务| 安卓开发者

于 2017-11-25T11:10:32.080 回答