This repository was archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Issue 1218: cancelling ui thread tasks #1395
Closed
Artyomcool
wants to merge
10
commits into
androidannotations:develop
from
Artyomcool:1218_cancelUiThreadTasks
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1b500aa
Add id param to @UiThread
WonderCsabo a880692
Add UiThreadExecutor which handles cancellation
WonderCsabo f7ec0c5
Handle @UiThread#id
WonderCsabo a02f0ec
Test UiThread task cancellation
WonderCsabo faaadbe
refactoring: reimplemented ui task cancellation in the more straight way
25fa9af
add: lock-free UIThreadHandler implementation
f0478d7
simple and effective implementation of cancelling ui tasks
9aa9580
add: cleaning up the map at cost of synchronization
bbe8c02
fix: synchronization for removing tokens
244bb93
refactoring: tests moved to test15 package and UI handlers hacks move…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...ons/androidannotations-api/src/main/java/org/androidannotations/api/UiThreadExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /** | ||
| * Copyright (C) 2010-2015 eBusiness Information, Excilys Group | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed To in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package org.androidannotations.api; | ||
|
|
||
| import android.os.Handler; | ||
| import android.os.Looper; | ||
| import android.os.Message; | ||
| import android.os.SystemClock; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This class provide operations for | ||
| * {@link org.androidannotations.annotations.UiThread UiThread} tasks. | ||
| */ | ||
| public class UiThreadExecutor { | ||
|
|
||
| private static final Handler HANDLER = new Handler(Looper.getMainLooper()) { | ||
| @Override | ||
| public void handleMessage(Message msg) { | ||
| Runnable callback = msg.getCallback(); | ||
| if (callback != null) { | ||
| callback.run(); | ||
| decrementToken((Token)msg.obj); | ||
| } else { | ||
| super.handleMessage(msg); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| private static final Map<String, Token> TOKENS = new HashMap<String, Token>(); | ||
|
|
||
| private UiThreadExecutor() { | ||
| // should not be instantiated | ||
| } | ||
|
|
||
| /** | ||
| * Store a new task in the map for providing cancellation. This method is | ||
| * used by AndroidAnnotations and not intended to be called by clients. | ||
| * | ||
| * @param id | ||
| * the identifier of the task | ||
| * @param task | ||
| * the task itself | ||
| * @param delay | ||
| * the delay or zero to run immediately | ||
| */ | ||
| public static void runTask(String id, Runnable task, long delay) { | ||
| if ("".equals(id)) { | ||
| HANDLER.postDelayed(task, delay); | ||
| return; | ||
| } | ||
| long time = SystemClock.uptimeMillis() + delay; | ||
| HANDLER.postAtTime(task, nextToken(id), time); | ||
| } | ||
|
|
||
| private static Token nextToken(String id) { | ||
| synchronized (TOKENS) { | ||
| Token token = TOKENS.get(id); | ||
| if (token == null) { | ||
| token = new Token(id); | ||
| TOKENS.put(id, token); | ||
| } | ||
| token.runnablesCount++; | ||
| return token; | ||
| } | ||
| } | ||
|
|
||
| private static void decrementToken(Token token) { | ||
| synchronized (TOKENS) { | ||
| if (--token.runnablesCount == 0) { | ||
| String id = token.id; | ||
| Token old = TOKENS.remove(id); | ||
| if (old != token) { | ||
| //a runnable finished after cancelling, we just removed a wrong token, lets put it back | ||
| TOKENS.put(id, old); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Cancel all tasks having the specified <code>id</code>. | ||
| * | ||
| * @param id | ||
| * the cancellation identifier | ||
| */ | ||
| public static void cancelAll(String id) { | ||
| Token token; | ||
| synchronized (TOKENS) { | ||
| token = TOKENS.remove(id); | ||
| } | ||
| if (token == null) { | ||
| //nothing to cancel | ||
| return; | ||
| } | ||
| HANDLER.removeCallbacksAndMessages(token); | ||
| } | ||
|
|
||
| private static class Token { | ||
| int runnablesCount = 0; | ||
| final String id; | ||
|
|
||
| private Token(String id) { | ||
| this.id = id; | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Artyomcool i almost forgot... I told you that we should run this in a try-finally block, so we make sure we remove the token even if the client code throws a
RuntimeException.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After you've got an exception here ui thread is effectively dead. Everything after that has no point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not agree. We store everything in static fields, so they are only cleared if the process is killed. If a
RuntimeExceptionhappen in the ui thread, theActivitywill likely crash, but it does not mean the process is killed. I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but you a not correct here. There is only one ui thread per process. When exception happens during looping, there is no way to restore it, even if you catch it in some way. Also, components never crash by themselves, only with a whole process. And it is very-very good thing, actually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to be sorry. :) You are right. I may have confused this with something
else, and just wrote down without really thinking it though.
I just tested this, and it goes up to ZygoteInit.main and dies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we could add some parameter to catch and log exceptions. Not sure if we should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.