java
public final class Pool {
private Pool() {
}
public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable, @NonNull final Consumer<Throwable> consumer) {
return scheduler.createWorker().schedule(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} catch (Exception e) {
try {
consumer.accept(e);
} catch (Exception ignored) {
}
}
}
});
}
public static Disposable submit(@NonNull Runnable runnable, @NonNull Consumer<Throwable> consumer) {
return submit(Schedulers.computation(), runnable, consumer);
}
public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable) {
return submit(scheduler, runnable, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("pool error", throwable);
}
});
}
public static Disposable submit(@NonNull final Runnable runnable) {
return submit(Schedulers.computation(), runnable);
}
public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable run, long delay, @NonNull final Consumer<Throwable> consumer) {
return scheduler.createWorker().schedule(new Runnable() {
@Override
public void run() {
try {
run.run();
} catch (Exception e) {
try {
consumer.accept(e);
} catch (Exception ignored) {
}
}
}
}, delay, TimeUnit.MILLISECONDS);
}
public static Disposable submit(@NonNull Runnable runnable, long delay, @NonNull Consumer<Throwable> consumer) {
return submit(Schedulers.computation(), runnable, delay, consumer);
}
public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable, long delay) {
return submit(scheduler, runnable, delay, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("pool error", throwable);
}
});
}
public static Disposable submit(@NonNull final Runnable runnable, long delay) {
return submit(Schedulers.computation(), runnable, delay);
}
public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable run, final long initialDelay, final long period, @NonNull final Consumer<Throwable> consumer) {
return scheduler.createWorker().schedulePeriodically(new Runnable() {
@Override
public void run() {
try {
run.run();
} catch (Exception e) {
try {
consumer.accept(e);
} catch (Exception ignored) {
}
}
}
}, initialDelay, period, TimeUnit.MILLISECONDS);
}
public static Disposable submit(@NonNull Runnable runnable, final long initialDelay, final long period, @NonNull Consumer<Throwable> consumer) {
return submit(Schedulers.computation(), runnable, initialDelay, period, consumer);
}
public static Disposable submit(@NonNull Scheduler scheduler, @NonNull final Runnable runnable, final long initialDelay, final long period) {
return submit(scheduler, runnable, initialDelay, period, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("pool error", throwable);
}
});
}
public static Disposable submit(@NonNull final Runnable runnable, final long initialDelay, final long period) {
return submit(Schedulers.computation(), runnable, initialDelay, period);
}
}
kotlin
fun submit(scheduler: Scheduler, runnable: () -> Unit, error: (t: Exception) -> Unit): Disposable {
return scheduler.createWorker().schedule {
try {
runnable()
} catch (e: Exception) {
error(e)
}
}
}
fun submit(runnable: () -> Unit, error: (t: Exception) -> Unit): Disposable {
return submit(Schedulers.computation(), runnable, error)
}
fun submit(scheduler: Scheduler, runnable: () -> Unit): Disposable {
return submit(scheduler, runnable) { Log.e("pool error", it) }
}
fun submit(runnable: () -> Unit): Disposable {
return submit(Schedulers.computation(), runnable)
}
fun submit(scheduler: Scheduler, run: () -> Unit, delay: Long, error: (t: Exception) -> Unit): Disposable {
return scheduler.createWorker().schedule({
try {
run()
} catch (e: Exception) {
error(e)
}
}, delay, TimeUnit.MILLISECONDS)
}
fun submit(runnable: () -> Unit, delay: Long, error: (t: Exception) -> Unit): Disposable {
return submit(Schedulers.computation(), runnable, delay, error)
}
fun submit(scheduler: Scheduler, runnable: () -> Unit, delay: Long): Disposable {
return submit(scheduler, runnable, delay) { Log.e("pool error", it) }
}
fun submit(runnable: () -> Unit, delay: Long): Disposable {
return submit(Schedulers.computation(), runnable, delay)
}
fun submit(scheduler: Scheduler, run: () -> Unit, initialDelay: Long, period: Long, error: (t: Exception) -> Unit): Disposable {
return scheduler.createWorker().schedulePeriodically({
try {
run()
} catch (e: Exception) {
error(e)
}
}, initialDelay, period, TimeUnit.MILLISECONDS)
}
fun submit(runnable: () -> Unit, initialDelay: Long, period: Long, error: (t: Exception) -> Unit): Disposable {
return submit(Schedulers.computation(), runnable, initialDelay, period, error)
}
fun submit(scheduler: Scheduler, runnable: () -> Unit, initialDelay: Long, period: Long): Disposable {
return submit(scheduler, runnable, initialDelay, period){Log.e("pool error", it)}
}
fun submit(runnable: () -> Unit, initialDelay: Long, period: Long): Disposable {
return submit(Schedulers.computation(), runnable, initialDelay, period)
}