-1

I have the following pieces of code:

1)

int operationCnt = 10;
for (int i = 0; i < operationCnt; i++) { // for every instruction
    final int curOperation = i;
    post(new Runnable() {
        @Override
        public void run() {
            // execute something on the UI thread

            if (curOperation == operationCnt) {
                // Execution has finished RUN THE NEXT PIECE OF CODE SOMEHOW
            }

        }
    })
}

2)

int operationCnt2 = 10;
for (int i = 0; i < operationCnt2; i++) { // for every instruction
    final int curOperation = i;
    post(new Runnable() {
        @Override
        public void run() {
            // execute something on the UI thread

            if (curOperation == operationCnt2) {
                // Execution has finished WRAP IT UP
            }

        }
    })
}

I want to make sure that operation 1 runs first, then right after it finishes operation 2 runs. Finally I'd love to be able to run some code after both have finished.

(I want those two to execute sequentially - strictly one after the other)

In other languages that is easy, but I'm not sure what the cleanest way would be to implement the same logic in Java?

Please shed some light.

Thank you

SudoPlz
  • 20,996
  • 12
  • 82
  • 123
  • there is a method in AsyncTask called as onPostExecute() here call your another async task – Deepak kaku May 16 '18 at 16:18
  • take this as an example -> https://stackoverflow.com/a/9671602/6142219 – Deepak kaku May 16 '18 at 16:19
  • I know that, but I thought AsyncTask was meant for background running services, and I'm not sure wether it's a good idea to dispatch a UI thread operation from the doInBackground method. What do you think? – SudoPlz May 16 '18 at 16:19
  • Use Java `Executor Framework`, `Future` will be usefull. – ADM May 16 '18 at 16:27

2 Answers2

0

Don't use operationCnt to control the thread-flow like that. Variable i use in runner must be final.

You can use Thread.join() to wait:

Runnable r = () -> // do something;
Thread t = new Thread(r);
t.start();
Thread t2 = new Thread(r2);
...
t.join();
t2.join();
t3.join();

// After the program reach here. All thread t_i are finished. You can start operation 2

If you don't want to block on main thread, then you can use CompletableFuture:

CompletableFuture.runAsync(() -> your first operation)
.thenRun(() -> Your second operation);
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
0

If you need to run one operation asynchronously, then run the second one after the first finishes, then why use two Runnables and two post calls? You can just combine them into one.

If you want to run the first N times

post(new Runnable() {
    @Override public void run() {
        for (int i = 0; i < 10; i++) {
            // do operation 1
        }
        for (int i = 0; i < 10; i++) {
            // do operation 2
        }
   }
});

or

post(new Runnable() {
    @Override public void run() {
        for (int i = 0; i < 10; i++) {
            // do operation 1
            // do operation 2
        }
   }
});
Leo Aso
  • 11,898
  • 3
  • 25
  • 46