The correct way to connect 2 asynchronous tasks in android

I have two asynchronous tasks, namely task 1 and task 2.

I need to start task 1 first and then task 2, but I do not want to bind them, invoking task 2 in the implementation of task 1 onPostExecute; because I use task 1 as an independent in other circumstances.

Is there a way for two asynchronous tasks to be defined without restriction from each other and to link them in certain circumstances?

Many thanks for your help.

+4
source share
2 answers

You can try something like this:

final Executor directExecutor = new Executor() { public void execute(Runnable r) { r.run(); } }; AsyncTask.execute(new Runnable() { task1.executeOnExecutor(directExecutor, params1); task2.executeOnExecutor(directExecutor, params2); }); 

I donโ€™t have an Android SDK right now, so I canโ€™t verify it.

0
source

You can do the following:

 YourAsyncClass1 thread1 = new YourAsyncClass1(); thread1.execute(inputArgument1); try { outputResult1 = thread1.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } if(outputResult1 == true /*Or expected result*/){ YourAsyncClass2 thread2 = new YourAsyncClass2(); thread2.execute(inputArgument2); } 
-one
source

Source: https://habr.com/ru/post/1493404/


All Articles