Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -32,6 +34,9 @@ public class BackgroundExecutor {
private static final String TAG = "BackgroundExecutor";

public static Executor DEFAULT_EXECUTOR = Executors.newScheduledThreadPool(2 * Runtime.getRuntime().availableProcessors());

public static Executor exceptionWatcherExecutor = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors());

private static Executor executor = DEFAULT_EXECUTOR;

private static final List<Task> tasks = new ArrayList<Task>();
Expand All @@ -51,7 +56,7 @@ public class BackgroundExecutor {
* @return Future associated to the running task
*/
private static Future<?> directExecute(Runnable runnable, int delay) {
Future<?> future = null;
final Future<?> future;
if (delay > 0) {
/* no serial, but a delay: schedule the task */
if (!(executor instanceof ScheduledExecutorService)) {
Expand All @@ -66,8 +71,38 @@ private static Future<?> directExecute(Runnable runnable, int delay) {
} else {
/* non-cancellable task */
executor.execute(runnable);
future = null;
}
}

/*
* Because ExecutorService.submit/schedule doesn't propagate exception
* during executions, we need to call futur.get() (in another thread
* pool) to check if an exception occured during execution. If yes then
* throw it so it could be caught by the system.
*/
if (future != null) {
exceptionWatcherExecutor.execute(new Runnable() {
@Override
public void run() {
try {
future.get();
} catch (InterruptedException e) {
Log.w(TAG, "Scheduled execution was interrupted", e);
} catch (CancellationException e) {
Log.w(TAG, "Watcher thread has been cancelled", e);
} catch (ExecutionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
if (e.getCause() instanceof Error) {
throw (Error) e.getCause();
}
}
}
});
}

return future;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -285,4 +286,40 @@ public void execute(Runnable command) {
}
}

/**
* Verify that exceptions thrown in threads from executorService are visible
* to exception handlers.
*
* Set a custom exception handler which stores the throwable and then check
* its content.
*/
@Test
public void propagateExceptionWithExecutorService() throws InterruptedException {
final Thread.UncaughtExceptionHandler originExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();

final TestExceptionHandler testExceptionHandler = new TestExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(testExceptionHandler);

BackgroundExecutor.setExecutor(new ScheduledThreadPoolExecutor(1));

activity.backgroundThrowException();
Thread.sleep(500);
Assert.assertNotNull("Exception should be propagated in @Backgound annotated methods", testExceptionHandler.throwable);

testExceptionHandler.throwable = null;

activity.backgroundDelayThrowException();
Thread.sleep(500);
Assert.assertNotNull("Exception should be propagated in @Backgound(delay) annotated methods", testExceptionHandler.throwable);
Thread.setDefaultUncaughtExceptionHandler(originExceptionHandler);
}

class TestExceptionHandler implements Thread.UncaughtExceptionHandler {
Throwable throwable;

@Override
public void uncaughtException(Thread thread, Throwable ex) {
throwable = ex;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.androidannotations.test15.instancestate.MySerializableBean;

import android.app.Activity;
import android.os.Looper;

@EActivity
public class ThreadActivity extends Activity {
Expand Down Expand Up @@ -145,6 +144,11 @@ void backgroundThrowException() {
throw new RuntimeException();
}

@Background(delay = 100)
void backgroundDelayThrowException() {
throw new RuntimeException();
}

@UiThread
void uiThreadThrowException() {
throw new RuntimeException();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.