0

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);
}
Groot
  • 177
  • 1
  • 2
  • 10

1 Answers1

1

Use documentation. All answers are here.

Future| Android developers

ExecutorService| Android developers

  • dude I change my code to this and everything is ok.http://codepad.org/C9swPyU5 . can you explain to me, does this method hold a main thread and after finish its job then resume the main thread? – Groot Nov 25 '17 at 11:56
  • A Future represents the result of an asynchronous computation.The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. So , answer to your question is NO , method fetchAllUsers() doesn't block Main thread. – Mykhailo Voloshyn Nov 25 '17 at 12:09
  • Ok, but why other code into main method `generatePage(int currentPage)` does not execute till my thread finished its job? if these codes into main method is executed I got error? – Groot Nov 25 '17 at 12:23