文档章节

Android 线程池工具类

wx1234
 wx1234
发布于 2017/09/11 10:23
字数 565
阅读 37
收藏 0

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

 

© 著作权归作者所有

共有 人打赏支持
wx1234
粉丝 1
博文 33
码字总数 8582
作品 0
杭州
私信 提问
Android多线程任务优化1:探讨AsyncTask的缺陷

导语:在开发Android应用的过程中,我们需要时刻注意保障应用的稳定性和界面响应性,因为不稳定或者响应速度慢的应用将会给用户带来非常差的交互体验。在越来越讲究用户体验的大环境下,用户...

Dean83
2012/06/14
0
0
Android异步任务(AsyncTask)的设计思想

AsyncTask在Android十分常用,那为什么如此常用呢,不用行不行呢,内部又是怎么实现的呢,为什么Java的API中没有这个类呢,看完本文后,你将会知道答案。 这里说有设计思想是我根据查看Andro...

爱捣鼓
2014/02/23
0
0
演化理解 Android 异步加载图片

在学习"Android异步加载图像小结"这篇文章时, 发现有些地方没写清楚,我就根据我的理解,把这篇文章的代码重写整理了一遍,下面就是我的整理。 下面测试使用的layout文件: 简单来说就是 Li...

Koon.LY
2012/05/04
0
1
Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式

android线程池的理解,晚上在家无事 预习了一下android异步加载的例子,也学习到了一个很重要的东东 那就是线程池+缓存 下面看他们的理解。 [size=1.8em]Handler+Runnable模式 我们先看一个并...

jxlgzwh
2013/07/25
0
0
一份关于 Java、Kotlin 与 Android 的学习笔记

JavaKotlinAndroidLearn 这是一份关于 Java 、Kotlin 、Android 的学习笔记,既包含对基础知识点的介绍,也包含对一些重要知识点的源码解析,笔记的大纲如下所示: Java 重拾Java(0)-基础知...

叶应是叶
08/08
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Redis异构集群之间数据迁移方案

一、Redis集群迁移工具 最近在做Redis数据迁移,网上找了两款开源的Redis迁移工具。 第一种:redis-port Codis官方提供的一个工具,redis-port是一个Redis工具,通过解析rdb文件,实现Redis...

IT--小哥
19分钟前
1
0
MySQL5.7源码安装

5、MySQL源码安装(centos7.5) 5.1 下载软件 官方自带(boost版本mysql) wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.20.tar.gz tar xf mysql-boost-5.7.20.tar.g......

hnairdb
23分钟前
1
0
解压zip包(zip4j)

1:引入zip4j_1.3.2.jar 2:源码如下: package test;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import net.lingal......

uug
26分钟前
1
0
MySQL 8在CentOS 7用rpm安装

1.配置MySQL yum安装源和安装: #配置MySQL5.7的安装源 #https://repo.mysql.com/mysql57-community-release-el7.rpm #https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rp......

MichaelShu
33分钟前
1
0
docker 安装zookeeper

1.下拉镜像: docker pull zookeeper 2.运行镜像: docker run --name myZookeeper --restart always -d zookeeper 此时,2181 2888 3888分别是zookeeper的(客户端端口,跟随端口,选择端口)...

狼王黄师傅
34分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部