mirror of https://github.com/tasks/tasks
Execute async IAB methods on single named thread
parent
2f3c0f9430
commit
45fe86d46f
@ -0,0 +1,38 @@
|
||||
package org.tasks;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
|
||||
import org.tasks.analytics.Tracker;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import static java.util.concurrent.Executors.newSingleThreadExecutor;
|
||||
|
||||
public class ErrorReportingSingleThreadExecutor implements Executor, Thread.UncaughtExceptionHandler {
|
||||
private final ExecutorService executorService;
|
||||
private final Tracker tracker;
|
||||
|
||||
public ErrorReportingSingleThreadExecutor(String nameFormat, Tracker tracker) {
|
||||
executorService = newSingleThreadExecutor(
|
||||
new ThreadFactoryBuilder()
|
||||
.setNameFormat(String.format("%s-%%d", nameFormat))
|
||||
.setUncaughtExceptionHandler(this)
|
||||
.build());
|
||||
this.tracker = tracker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Runnable runnable) {
|
||||
try {
|
||||
executorService.execute(runnable);
|
||||
} catch (Exception e) {
|
||||
tracker.reportException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uncaughtException(Thread thread, Throwable throwable) {
|
||||
tracker.reportException(thread, throwable);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue