Android线程池管理-ThreadPoolHelp

在app项目中多线程使用越来越频繁,线程管理非常重要。根据工作以前的项目经验,最近整理了一份线程池管理工具库。



ThreadPoolHelp是一款Android平台下简单易用的线程池管理工具,包大小只有9K,能满足常用的多线程管理需求。

1、简单用法

a) 使用默认线程池

这是使用线程池最简单的方式,使用默认线程池处理线程任务。

new ThreadTaskObject() {
    @Override
    public void run() {
        //线程执行体 
    }
}.start();

b) 指定线程池

通过指定线程池,可以把不同功能类型的线程分组,比如指定线程池“newPool”,线程任务将在线程池“newPool”中运行。

new ThreadTaskObject("newPool") {
    @Override
    public void run() {
        //线程执行体 
    }
}.start();

以上两种线程池的用法能满足开发基本需求,如需要更加详细的配置,参考进阶用法。

2、进阶用法

a) 创建一个可缓存线程池

该线程池的特点是:如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。

默认线程池

ThreadPoolHelp.Builder
.cached()
.builder()
.execute(new Runnable() {
    @Override
    public void run() {
        //线程执行体
 });

指定线程池

其中“poolName”为线程池名称

ThreadPoolHelp.Builder
.cached()
.name("poolName")
.builder()
.execute(new Runnable() {
    @Override
    public void run() {
        //线程执行体
});

b) 创建一个定长线程池

该线程池的特点是,可控制线程最大并发数,超出的线程会在队列中等待。

默认线程池

其中size为固定线程池大小

ExecutorService executorService = ThreadPoolHelp.Builder
                  .fixed(size)
                  .builder();
executorService.execute(new Runnable() {
    @Override
    public void run() {
        //线程执行体
});

指定线程池

其中“poolName”为线程池名称,size为固定线程大小

ExecutorService executorService = ThreadPoolHelp.Builder
                  .fixed(size)
                  .name("poolName")
                  .builder();

executorService.execute(new Runnable() {
    @Override
    public void run() {
        //线程执行体
});

c) 创建一个单线程化的线程池

该线程池的特点是,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

默认线程池

ThreadPoolHelp.Builder
.single()
.builder()
.execute(new Runnable() {
    @Override
    public void run() {
        //线程执行体
});

指定线程池

其中“poolName”为线程池名称

ThreadPoolHelp.Builder
.single()
.name("poolName")
.builder()
.execute(new Runnable() {
    @Override
    public void run() {
        //线程执行体
});

d) 创建一个定长线程池定时任务

该线程池的特点是,支持定时及周期性任务执行。

ScheduledExecutorService executorService = ThreadPoolHelp.Builder
                      .schedule(size)
                      .scheduleBuilder();
executorService.schedule(new Runnable() {
    @Override
    public void run() {
        //线程执行体
    }, 3000, TimeUnit.MILLISECONDS);

e) 创建一个自定义的线程池

该线程池的特点是,如果以上都不能满足需求,开发者可以根据需求自己设置参数。

ExecutorService executorService = ThreadPoolHelp.Builder
                  .custom(corePoolSize, 
                      maximumPoolSize, 
                      keepAliveTime, 
                      unit, 
                      workQueue)
                  .builder());
executorService.execute(new Runnable() {
    @Override
    public void run() {
        //线程执行体
    });

源码地址

转载请写明出处:http://www.luoxudong.com/?p=12

有27人对 “Android线程池管理-ThreadPoolHelp”留言了

  1. The EU’s medicines regulator says unusual blood clots should be listed as a very rare side effect of the AstraZeneca vaccine for Covid-19.
    After a study looking at 86 European cases, the European Medicines Agency (EMA) concluded the benefits of the vaccine outweighed the risk.

  2. Excellent blog! Do you have any hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there
    that I’m totally overwhelmed .. Any ideas? Thanks!

  3. Thank you for the auspicious writeup. It in fact was a amusement account it.

    Look advanced to far added agreeable from you!
    By the way, how can we communicate?

  4. Hi there, just became aware of your blog through Google, and found that it’s really informative.
    I am gonna watch out for brussels. I’ll appreciate if you continue this
    in future. Numerous people will be benefited from your writing.
    Cheers!

  5. What’s up, I log on to your blogs like every week.
    Your story-telling style is witty, keep up the good work!

  6. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get bought an impatience over that you
    wish be delivering the following. unwell unquestionably come
    further formerly again since exactly the same nearly a lot
    often inside case you shield this increase.

  7. Hey I am so grateful I found your site, I really found you by accident, while I was searching
    on Yahoo for something else, Anyhow I am here now and would just like to say kudos
    for a marvelous post and a all round interesting blog (I also
    love the theme/design), I don’t have time to go through it all at the minute but I have saved it and also
    added in your RSS feeds, so when I have time I
    will be back to read much more, Please do keep up the
    awesome work.

  8. Hi there! Do you know if they make any plugins to protect
    against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any tips?

  9. I have fun with, result in I found exactly what
    I used to be having a look for. You have ended my 4 day lengthy
    hunt! God Bless you man. Have a great day. Bye

  10. I do not even know how I ended up here, but I thought this post was great.
    I do not know who you are but definitely you are going to a famous blogger if you are not already
    😉 Cheers!

  11. I was suggested this web site via my cousin. I’m not sure whether this post
    is written by him as nobody else understand such exact
    about my problem. You’re amazing! Thanks!

  12. Whats up very nice site!! Man .. Beautiful .. Wonderful ..
    I’ll bookmark your website and take the feeds additionally?
    I am glad to find a lot of useful info right here in the publish, we want develop extra
    techniques on this regard, thanks for sharing. . . .
    . .

  13. Does your website have a contact page? I’m having a tough time locating it but, I’d like
    to send you an e-mail. I’ve got some recommendations for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it
    develop over time.

  14. certainly like your web site but you have to check the spelling on several of your posts.
    A number of them are rife with spelling problems and I to
    find it very bothersome to tell the reality then again I’ll definitely
    come back again.

  15. Howdy! This post could not be written any better!
    Going through this post reminds me of my previous roommate!
    He always kept preaching about this. I most certainly will
    forward this information to him. Pretty sure he will have a very good read.
    Thank you for sharing!

  16. Your method of explaining the whole thing in this article is
    really nice, all be able to without difficulty be aware of it, Thanks a lot.

  17. It’s actually very complex in this active life to listen news on Television, so I
    just use internet for that reason, and get the most up-to-date news.

发表评论

邮箱地址不会被公开。