Code example:

for (int i = 0; i < values.properties.length; i++) { String text = value.properties[i].type; if (text.equals("one")) { // тут что-либо делаем (не поток) } if (text.equals("two")) { // тут тоже что либо делаем (не поток) } if (text.equals("thread")) { // а вот тут уже создаем и запускаем поток new CreateSpinner().execute(); } } 

What is your own question: I need the cycle not to continue until the end of the flow in the third condition.

  • 2
    I think you need to start a loop inside AsyncTask, and not create a bunch of threads, and your whole problem will be solved. At least for the task you described in the question. - andreich

4 answers 4

The fact that you have changed a bit of the question, does not cancel my comment, do everything completely in AsyncTask, if it doesn’t fit, since I didn’t understand something, this is what occurred to me:

Try to implement through a queue, instead of an array of value.properties, and recursion.
This queue is passed to the doSomeThing (...) method.
Accordingly, the current item that comes from the queue will be value.properties [i], and after use, the doSomeThing (...) method is deleted and called again.

And in the last branch, AsyncTask is launched, and after it is executed, doSomeThing (...) is called in onPostExecute (...).

Something like this

  public void doSMT(Queue<String> q) { String text = q.peek(); if (text.equals("one")) { // тут что-либо делаем (не поток) //....... //....... doSMT(q); } if (text.equals("two")) { // тут тоже что либо делаем (не поток) //....... //....... doSMT(q); } if (text.equals("thread")) { // а вот тут уже создаем и запускаем поток new CreateSpinner(q).execute(); } } class CreateSpinner extens AsyncTask<Void,Void,Void> { private Queue<String> q public CreateSpinner(Queue<String> q) { this.q = q; } @Override public Void doinbackgroudn(Void... p) { // тут тоже что либо делаем //....... //....... } @Override public void onPostExecute() { // тут тоже что либо делаем //....... //....... doSMT(q); } } 

The decision does not pull on elegance, but should work as you need.

    And what is the actual question? we describe our AsyncTask in doInBackground send a request to the server, do not forget to catch exceptions - everything seems to be

      The threads in the android are executed sequentially rather than in parallel (for this api 11+ there is a myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); method myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); ), but if you need a controlled sequence, send timeout requests or after receiving a response from the server.

      • a little wrong formulated the question. updated post. - dexter

      If the only thing that would be to write the results of the query in the right sequence, then why not just assign an index to each instance of the async, and not write the result to onPostExecute () in the array under this index?