0
我有一个方法。在这个方法中,我由Executors运行一个任务。其工作是从服务器获取数据并填写结果Arraylist。countinue Executors.newFixedThreadPool完成其作业后的方法
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.how coud我知道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);
}
哥们我我的代码更改为这一切都是ok.http://codepad.org/C9swPyU5。你能向我解释一下,这个方法是否存在一个主线程,并且在完成它的工作之后又恢复主线程? – Groot
Future表示异步计算的结果。可调用接口类似于Runnable,因为它们都是为其实例可能由另一个线程执行的类设计的。所以,回答你的问题是NO,方法fetchAllUsers()不会阻塞主线程。 –
好的,但为什么其他代码到主线程方法'generatePage(int currentPage)'不执行,直到我的线程完成其工作?如果这些代码进入主要方法执行我得到错误? – Groot