Skip to content

Commit 31adbd8

Browse files
author
Yoann Delouis
committed
Use ScheduledExecutorService instead of Handler
1 parent 044017c commit 31adbd8

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/api/BackgroundExecutor.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,27 @@
1717

1818
import java.util.concurrent.Executor;
1919
import java.util.concurrent.Executors;
20+
import java.util.concurrent.ScheduledExecutorService;
21+
import java.util.concurrent.TimeUnit;
2022

2123
public class BackgroundExecutor {
2224

2325
private static Executor executor = Executors.newCachedThreadPool();
26+
private static ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
2427

2528
public static void execute(Runnable runnable) {
2629
executor.execute(runnable);
2730
}
28-
31+
2932
public static void setExecutor(Executor executor) {
3033
BackgroundExecutor.executor = executor;
3134
}
3235

36+
public static void executeDelayed(Runnable runnable, long delay) {
37+
scheduledExecutor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
38+
}
39+
40+
public static void setScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
41+
BackgroundExecutor.scheduledExecutor = scheduledExecutor;
42+
}
3343
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/BackgroundProcessor.java‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static com.sun.codemodel.JExpr._new;
1919
import static com.sun.codemodel.JExpr.lit;
2020

21-
import java.lang.annotation.Annotation;
2221
import javax.lang.model.element.Element;
2322
import javax.lang.model.element.ExecutableElement;
2423

@@ -30,10 +29,8 @@
3029
import com.sun.codemodel.JClassAlreadyExistsException;
3130
import com.sun.codemodel.JCodeModel;
3231
import com.sun.codemodel.JDefinedClass;
33-
import com.sun.codemodel.JExpr;
3432
import com.sun.codemodel.JInvocation;
3533
import com.sun.codemodel.JMethod;
36-
import com.sun.codemodel.JMod;
3734

3835
public class BackgroundProcessor implements DecoratingElementProcessor {
3936

@@ -61,19 +58,16 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
6158
long delay = annotation.delay();
6259

6360
JClass backgroundExecutorClass = holder.refClass(BackgroundExecutor.class);
64-
JInvocation executeCall = backgroundExecutorClass.staticInvoke("execute").arg(_new(anonymousRunnableClass));
61+
JInvocation executeCall;
6562

6663
if (delay == 0) {
67-
delegatingMethod.body().add(executeCall);
64+
executeCall = backgroundExecutorClass.staticInvoke("execute").arg(_new(anonymousRunnableClass));
6865
} else {
69-
JDefinedClass anonymousExecutorRunnableClass = helper.createAnonymousRunnableClass(holder, executeCall);
70-
if (holder.handler == null) {
71-
JClass handlerClass = holder.classes().HANDLER;
72-
holder.handler = holder.generatedClass.field(JMod.PRIVATE, handlerClass, "handler_", _new(handlerClass));
73-
}
74-
delegatingMethod.body().invoke(holder.handler, "postDelayed").arg(_new(anonymousExecutorRunnableClass)).arg(lit(delay));
66+
executeCall = backgroundExecutorClass.staticInvoke("executeDelayed").arg(_new(anonymousRunnableClass)).arg(lit(delay));
7567
}
7668

69+
delegatingMethod.body().add(executeCall);
70+
7771
}
7872

7973
}

0 commit comments

Comments
 (0)