|
19 | 19 | import java.lang.annotation.Retention; |
20 | 20 | import java.lang.annotation.RetentionPolicy; |
21 | 21 | import java.lang.annotation.Target; |
| 22 | +import java.util.concurrent.Future; |
| 23 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
22 | 24 |
|
23 | 25 | /** |
24 | | - * Should be used on method that must be run in a background thread. This method |
25 | | - * must belong to an activity annotated with @Layout. |
| 26 | + * Should be used on method that must be run in a background thread. |
| 27 | + * <p/> |
| 28 | + * The annotated method MUST return void and MAY contain parameters. |
| 29 | + * <p/> |
| 30 | + * The generated code is based on {@link BackgroundExecutor} methods. |
26 | 31 | * |
| 32 | + * |
| 33 | + * <h2>Cancellation</h2> |
| 34 | + * Since 3.0, you're able to cancel a background task by calling |
| 35 | + * <code>BackgroundExecutor.cancelAll("id")</code> where "id" matches the |
| 36 | + * {@link #id()} value. |
| 37 | + * |
| 38 | + * <p/> |
| 39 | + * <blockquote> |
| 40 | + * |
| 41 | + * <b>Example</b> : |
| 42 | + * |
| 43 | + * <pre> |
| 44 | + * @EBean |
| 45 | + * public class MyBean { |
| 46 | + * private static final String TASK_ID = "task1"; |
| 47 | + * |
| 48 | + * @Background(id = TASK_ID) |
| 49 | + * void launchTask() { |
| 50 | + * // ... |
| 51 | + * } |
| 52 | + * |
| 53 | + * void stopTask() { |
| 54 | + * BackgroundExecutor.cancelAll(TASK_ID); |
| 55 | + * } |
| 56 | + * |
| 57 | + * } |
| 58 | + * </pre> |
| 59 | + * |
| 60 | + * </blockquote> |
| 61 | + * <p/> |
| 62 | + * <b>Note</b>: Cancellation may or may not be successful. If the task wasn't |
| 63 | + * executed yet, it will be removed from the pool. But it could fail if task has |
| 64 | + * already completed, has already been cancelled, or could not be cancelled for |
| 65 | + * some other reason. See {@link Future#cancel(boolean)} for more information. |
| 66 | + * |
| 67 | + * |
| 68 | + * <h2>Execution flow</h2> |
| 69 | + * By default, all tasks will be put in a {@link ScheduledThreadPoolExecutor} |
| 70 | + * with a core pool size of <code>2 * numberOfCpu</code>. Which means that |
| 71 | + * background methods will be executed in <b>PARALLEL</b>. You can change this |
| 72 | + * by calling <code>BackgroundExecutor.setExecutor(...)</code>. |
| 73 | + * |
| 74 | + * If you want execute ALL background methods SEQUENTIALLY, the best way is to |
| 75 | + * change the executor of {@link BackgroundExecutor} to a |
| 76 | + * {@link ScheduledThreadPoolExecutor} with a core pool size of <code>1</code>. |
| 77 | + * <p/> |
| 78 | + * If you want execute some background methods SEQUENTIALLY, you should simply |
| 79 | + * use {@link #serial()} field. All task with the same serial key will be |
| 80 | + * executed sequentially. |
| 81 | + * |
| 82 | + * <p/> |
| 83 | + * <blockquote> |
| 84 | + * |
| 85 | + * <b>Example 1</b> (all tasks executed sequentially) : |
| 86 | + * |
| 87 | + * <pre> |
| 88 | + * @EBean |
| 89 | + * public class MyBean { |
| 90 | + * |
| 91 | + * static { |
| 92 | + * BackgroundExecutor.setExecutor(Executors.newScheduledThreadPool(1)); |
| 93 | + * } |
| 94 | + * |
| 95 | + * private int i = 0; |
| 96 | + * |
| 97 | + * void launchTasks() { |
| 98 | + * backgroundTask(); |
| 99 | + * backgroundTask(); |
| 100 | + * backgroundTask(); |
| 101 | + * } |
| 102 | + * |
| 103 | + * @Background |
| 104 | + * void backgroundTask() { |
| 105 | + * Log.i("AA", "i = ", i++); |
| 106 | + * } |
| 107 | + * } |
| 108 | + * </pre> |
| 109 | + * |
| 110 | + * <b>Example 2</b> (some tasks executed sequentially) : |
| 111 | + * |
| 112 | + * <pre> |
| 113 | + * @EBean |
| 114 | + * public class MyBean { |
| 115 | + * |
| 116 | + * private int i = 0; |
| 117 | + * |
| 118 | + * void launchTasks() { |
| 119 | + * backgroundTask(); |
| 120 | + * backgroundTask(); |
| 121 | + * backgroundTask(); |
| 122 | + * } |
| 123 | + * |
| 124 | + * @Background(serial = "sequence1") |
| 125 | + * void backgroundTask() { |
| 126 | + * Log.i("AA", "i = ", i++); |
| 127 | + * } |
| 128 | + * } |
| 129 | + * </pre> |
| 130 | + * |
| 131 | + * </blockquote> |
| 132 | + * |
| 133 | + * |
| 134 | + * <h2>Delay</h2> |
| 135 | + * Sometimes you may want to delay execution of a background method. To do so, |
| 136 | + * you should use the {@link #delay()} field. |
| 137 | + * <p/> |
| 138 | + * <b>Example</b> : |
| 139 | + * |
| 140 | + * <pre> |
| 141 | + * @EBean |
| 142 | + * public class MyBean { |
| 143 | + * |
| 144 | + * @Background(delay = 2000) |
| 145 | + * void backgroundTask() { |
| 146 | + * // ... |
| 147 | + * } |
| 148 | + * } |
| 149 | + * </pre> |
| 150 | + * |
| 151 | + * </blockquote> |
| 152 | + * |
| 153 | + * |
| 154 | + * @see UiThread |
| 155 | + * @see BackgroundExecutor |
27 | 156 | */ |
28 | 157 | @Retention(RetentionPolicy.CLASS) |
29 | 158 | @Target(ElementType.METHOD) |
|
40 | 169 | **/ |
41 | 170 | String id() default ""; |
42 | 171 |
|
43 | | - /** Minimum delay, in milliseconds, before the background task is executed. */ |
| 172 | + /** |
| 173 | + * Minimum delay, in milliseconds, before the background task is executed. |
| 174 | + */ |
44 | 175 | int delay() default 0; |
45 | 176 |
|
46 | 177 | /** |
|
0 commit comments