I have a method. inside this method I run a task by Executors. its work is fetch data from server and fill an results Arraylist.
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));
}
after filling results Arraylist i have to use this arraylist.how coud i know where executorService finished its job and I can continue my method again?
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);
}