Android 线程池工具类

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)
}

 

转载于:https://my.oschina.net/SSS555/blog/1534941

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值