Here is a task initialization code

CoordsAnalizatorTask coordsAnalizatorTask = new CoordsAnalizatorTask(); 

Next, I run it with parameters

 coordsAnalizatorTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new Double[]{coords_w, coodrs_l, angle}); 

In essence, AsyncTask.SERIAL_EXECUTOR indicates that tasks will be performed sequentially, which I have no doubt. I found an article where there is an example and it is clearly visible that calls are made sequentially (at the very bottom of the article) i.e. if i send tasks like this

 coordsAnalizatorTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new Double[]{coords_w, coodrs_l, angle}); coordsAnalizatorTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new Double[]{coords_w, coodrs_l, angle}); 

Both of them should be transferred to AsyckTask , but they will not be executed in parallel, but in series, which is what I need, but performing such actions gives me an error

 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: FATAL EXCEPTION: main 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: Process: com.example.miroshnichenko.mylineviewexample, PID: 21238 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miroshnichenko.mylineviewexample/com.example.miroshnichenko.mylineviewexample.MainActivity}: java.lang.IllegalStateException: Cannot execute task: the task is already running. 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:177) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5942) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Cannot execute task: the task is already running. 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:576) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at com.example.miroshnichenko.mylineviewexample.CoordinatesAnalizator.sendCoords(CoordinatesAnalizator.java:63) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at com.example.miroshnichenko.mylineviewexample.NmeaParser.addNmeaByteArray(NmeaParser.java:62) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at com.example.miroshnichenko.mylineviewexample.MainActivity.onCreate(MainActivity.java:56) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6289) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:177) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5942) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 12-01 12:40:41.811 21238-21238/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

As if to say that I cannot use AsyncTask while it is working, which is very strange, since in the example it is clearly written that sequential execution with such a call should be and be without errors.

The essence of the task is such that there is a stream that constantly parses the coordinates and throws them into AsyncTask , then the task should work with them consistently, i.e. handle one task after another, but those on, such a problem arose. Prompt a solution to the problem or suggest an alternative. Thank you in advance!

  • It is impossible to add the same asyncaster instance to run 2 times. Just create a new instance of this task - Vladyslav Matviienko
  • @metalurgus Yes, thanks, I already saw my mistake. And with this approach, the tasks will be consistently executed? Then I just don’t quite understand how when creating a new object and launching it, AsyncTask will understand that another task has already been launched there, one thing, when you transfer all tasks to the method, then the object will be taste, who will be the first to come , and others in the queue, and then the creation of a new object is trivial, from where it will be aware of the implementation of another? - BORSHEVIK
  • @BORSHEVIK they will be executed sequentially on SERIAL_EXECUTOR. for example, there is an X and Y object and you call X.executeOnExecutor (SERIAL_EXECUTOR and Y.executeOnExecutor (SERIAL_EXECUTOR), the objects are different, and the execution environment is the same - the static Executor from the AsyncTask class - iksuy
  • @iksuy I understand there is an instant executer inside AsyncTask? - BORSHEVIK
  • @BORSHEVIK yes, that's right. I wrote in my answer, do not hesitate to look at the source of libraries in such cases, it helps a lot for a deeper understanding of what is happening and in general for professional development :) - iksuy

2 answers 2

This is the same task.

 coordsAnalizatorTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new Double[]{coords_w, coodrs_l, angle}); 

The coordsAnalizatorTask object coordsAnalizatorTask already running, and you are trying to start it again. It is necessary either to wait for this task to complete, or to create a new object each time (depending on what you need)

I recommend in such cases to watch source codes of library methods which you use and in which Exception takes off. In this case, you can see when IllegalStateException is IllegalStateException inside an executeOnExecutor

    For sequentially performed tasks, I would look in the direction of the IntentService

    • I know about him, but there are small dances with tambourines, if it is necessary to get the result from the service - BORSHEVIK
    • EventBus to help you! - Alex Ziko
    • Thank you for libu, but in fact, I danced with a tambourine when I meant binding - BORSHEVIK