-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Cancel background tasks #569
Conversation
A ScheduledExecutorService *is* an Executor, so we can use the same to avoids to create too many threads. The execute(runnable) method becomes a particular case of execute(runnable, delay), with delay=0. As the user can change the executor, delayed executions are not supported if the given executor does not support scheduling. This unification paves the way for adding serial execution feature.
Adds an optional serial attribute to @background: @background(serial="some_id") This idea comes from androidannotations#309: androidannotations#309 The principle of this implementation is to keep a separate queue for each serial identifier, and to give each task to the (unique) executor only after the previous task with the same serial identifier (if any) has completed execution. It guarantees that all tasks with the same serial identifier will be called in the order, one at a time (but not necessarily on the same thread). It is still compatible with the delay attribute. For example: @background(serial="some_id", delay=2000) executes the task in background after at least 2 seconds *and* after the previous task requested with serial="some_id" (if any) have completed execution.
Test if actions are sequential as expected.
Avoid try/catch InterruptedException of Thread.sleep().
The previous implementation used a set for serial running and a map of lists of tasks for managing the serial queues. In practice, there will be very few parallel tasks, using maps and creating/destroying lists is complex and unefficient. A better approach is to use only a list of tasks (only the ones we need to keep, having a non-null serial) and run through it sequentially for retrieving tasks. Moreover, it is more general, and paves the way for adding a task cancellation feature.
Extract the surround-with-try-catch part of the anonymous Runnable generation, in order to reuse it for generating other anonymous classes.
Instead of create a new Runnable instance which will be wrapped by a Task instance, directly create a Task (which is a Runnable).
Adds an optional id attribute to @background: @background(id="some_id") The user is able to cancel all background tasks having a specified id: BackgroundExecutor.cancelAll("some_id");
Tests the serialization and cancellation of background tasks.
If the executor is replaced by another not supporting cancellation (not extending ExecutorService), it failed the first time a task with a non-null id was requested. Now, it just warns during cancellation.
|
Very cool, thx |
|
@melnikovdv I didn't see the issue #478 (because it was closed). I reference it here. |
|
sorry, it is my fault to compile with duplicate jars |
|
I was looking at your PR and I saw all the comments in the code and commit descriptions. I was just... wow. Thanks for that 👍 However, we have to do lots of tests on this feature |
This bug was fixed in commit 5254f56. If you test
I solved it a similar way, I just used 0 as special value instead of using a
Another solution could be to use I accept your solution too, let me know what do you prefer. |
|
Thank you for contributing this and for going through all these reviews. I believe this is a very sensitive part of the code, and it's hard to get threading right, which is why everybody gets very sensitive about this. This code looks good to me. What I'm missing now is more code using this. Ideally we'd need to put that into apps, see what are the use cases, and if that fits with what we usually want to do. I think it makes sense to bring both pull requests into the code, but we should also start testing that in apps. |
|
Thanks for this feature |
|
Great! Thank you for your time and for merging ;-) |
Like someone asked on the mailing list, I think it would be useful to be able to cancel background tasks, typically in
onStop():I implemented it over my changes adding the background execution serialization feature (I changed a bit the implementation).
@Backgroundnow supportsid(used for cancellation),delay(was already implemented) andserial(for serializing tasks) attributes, and any association of them.As the background execution feature must be reliable, and the interactions between serializing and cancelling must behave correctly, I wrote a set of functional tests.