ThreadPoolExecutor 快速实际应用

简要:

本教程将会告诉你,怎样在Android上用多线程技术 同时执行 多个 耗时长 的任务。

0. 背景绍介(为什么用ThreadPoolExecutor)

之前用的是AsyncTask,但是这个不能用于时间过长的并行任务,我是出了问题:

02-23 13:40:13.922 13922-13922/? A/DEBUG: pid: 13505, tid: 13699, name: AsyncTask #2  >>> com.example.rp.my_cv_project <<<
                                          signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1684
                                              r0 c8756800  r1 00001680  r2 00000001  r3 9395e9f8
                                              r4 00000001  r5 12c32ac0  r6 6fe50310  r7 12c3db50
                                              r8 12c3db40  r9 dea93e00  sl 12c06688  fp 00000000
                                              ip e7cd59cc  sp bf27f3f0  lr 729176d7  pc e7de5b92  cpsr 680f0030
02-23 13:40:15.403 495-495/? E/Layer: [Application Error: com.example.rp.my_cv_project] rejecting buffer: bufWidth=1333, bufHeight=548, front.active.{w=169, h=169}

你跟我说,这玩意谁看得懂啊?!

PC地址也没有,我没办法用add2line吧......

所以,我打算改成ThreadPoolExecutor。为什么用这个呢?缘于官方的文档:

To automatically run tasks as resources become available, or to allow multiple tasks to run at the same time (or both), you need to provide a managed collection of threads. To do this, use an instance of ThreadPoolExecutor

这里:

Android官方教程

顺便,这里还说了,IntentService不适用于多线程同步执行的。它只适用于单个的:

If you want to run a task repeatedly on different sets of data, but you only need one execution running at a time, an IntentService suits your needs.

好,那么就开始用吧,我搜了一下ThreadPoolExecutor,哗啦啦出来一堆东西,但就是没有我想要的。

是的,你们虽然把原理讲的明明白白,什么茴香豆的四种写法...啊不,是常用的四种线程池,但是能不能有更直接的,能让我拿来就用的教程?

翻了半天,找到了这个博客:参考

那我就在它基础上,写一个我本来想要第一眼就看到的,简单易懂的ThreadPoolExecutor应用实例吧:

1. 需求

现在我想在主线程里将十张图片(Bitmap)同时用多线程来进行处理。

2. 例程

首先,我们有一个负责处理图片的class,叫它process吧:

public class process implements Runnable, Serializable {
	Bitmap bitmap;
	int other_parameters;
	// 利用构造函数添加输入的参数
	public process(Bitmap bitmap, int other_parameters){
		this.bitmap = bitmap;
		this.other_parameters = other_parameters;
	}
	// 处理函数,里面利用输入的bitmap以及other_parameters进行处理。
	public void real_process(){
		// write your own code here~~~
		// 增加一个callback来返回主线程~
		// 具体关于callback怎么用,请参考代码块下面的网站~
	}
	@Override
    public void run() {
        real_process();
    }
}

简单!猴子也能学会的callback用法!它让你可以在线程结束之后立刻获得通知以及执行的结果,不需要用while之类的蠢法子,美滋滋。

然后,我们在主线程中,先对线程池进行一个创建:

private ThreadPoolExecutor SetThreadPoolExecuteor(){
        /*
         * Number of cores to decide the number of threads
         */
        final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
        // 构造一个线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(NUMBER_OF_CORES*2
                , NUMBER_OF_CORES*2, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        return threadPool;
    }

至于里面的参数,我只能说解释已经烂大街了。我从那些博客里净看这群参数的解释了:

这个这个以及这个

然后,接下来是调用:

// getBitmapArrayList,获取bitmap的arraylist
ArrayList<Bitmap> res_split = getBitmapArrayList();
// 获得其他参数
int other_parameters = get_other_parameters();
// 创建线程池
ThreadPoolExecutor myTPE = SetThreadPoolExecuteor();
for(int i=0;i<res_split.size();i++){
    Bitmap img = res_split.get(i);
    // 这句是开始执行~!
    myTPE.execute(new process(img,other_parameters));
}

恭喜!你已经完全准备好了!接下来就自己试试吧!乱七八糟的参数再根据需求调一调,美滋滋。

如果本文对你有用,我会非常开心的O(∩_∩)O~~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值