This is fragment of my code:
AppExecutors.getInstance().diskIO().execute(new Runnable() {
@Override
public void run() {
// work 1
}
});
JSONArray apiResponseGet = ApiHelper.getInstance().getData();
if(apiResponseGet == null)
throw new Exception("Api call failed");
AppExecutors.getInstance().diskIO().execute(new Runnable() {
@Override
public void run() {
// work 2
}
});
What I except this code to do is to launch work 1
, then, while work 1
is executing, make request to api, and after api call finishes save it's results in work 2
.
The problem here is that work 2
has to be executed after work 1
is done. If work 2
would start before work 1
is finished, this could cause my app to misbehave.
Usually call to API will take much longer than work 1
, but let's assume for now that api call has finished, and work 1
is still running. Since diskIO is SingleThreadExecutor
, does that mean, that since it's single thread, in this case work 2
will have to wait until work 1
finishes?
And if answer is no, how to make sure that when work 2
starts, work 1
is already done?
From SingleThreadExecutor:
Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.
So in your case work 2
will have to wait until work 1
finishes.