Android 线程池工具类
博客专区 > wx1234 的博客 > 博客详情
Android 线程池工具类
wx1234 发表于3个月前
Android 线程池工具类
  • 发表于 3个月前
  • 阅读 14
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 基于RX 写着玩儿

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

 

共有 人打赏支持
粉丝 2
博文 32
码字总数 8245
×
wx1234
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: