0

I have 3 asynchronous methods:

1 - Update Data
2 - Validate Data
3 - Enter

Currently I have the class as follows:

public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_sync);
....

new Update (). execute ();
new Validate () .execute ();
new Enter (). execute ();
}

It is assumed that they all have to execute at the same time however they expect to have an answer from the previous one.

How can I make them all run at the same time without waiting for the previous one to finish?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Juan Tobon
  • 45
  • 7
  • Best way I believe would be [ExecutorService.invokeAll();](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)) – Cowboy Farnz May 15 '18 at 14:49
  • I use asynchronous classes Example: private class Update extends AsyncTask { ... } – Juan Tobon May 15 '18 at 15:37

2 Answers2

0

You should run each method in a separate thread. Make Update, Validate, Enter implement Runnable. In the run() method call execute() or rename execute() to run(). Then

ExecutorService pool = Executors.newFixedThreadPool(poolSize);;
pool.execute(new Update());
pool.execute(new Validate());
pool.execute(new Enter());
Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
cecunami
  • 957
  • 5
  • 12
0

You can use .executeOnExecutor()* instead of .execute() to run multiple AsynchTask at the same time like this:

* Works from API level 11 or onward

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
     new Update().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
     new Validate().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
     new Enter().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 else {
     new Update().execute();
     new Validate().execute();
     new Enter().execute();
}
Deven
  • 3,078
  • 1
  • 32
  • 34