AsyncTask使用JDK提供的线程池,等待任务不能超过128否则将出现异常。
这是一种极端的情况,通常很少遇到,但是当手机网络比较差时,拖动ListView或者GrideView异步加载数据,有可能会出现。
我遇到的情况是在一个ListView里面用异步获取图片显示。在adapter里面获取图片的时候就会开一个asynctask去获取,但是当拖动listview或者gridview的时候
线程池报java.util.concurrent.RejectedExecutionException异常
我的解决方法是,还没加载图片的时候显示默认图片,创建adapter的时候开一个Asynctask,在后台用一个for循环,循环获取图片或者缩略图,存放在一个hashMap里面,后台任务完成之后在一个循环显示listview的缩略图。
int getsize = urllist.size();
for(int i=0;i<getsize;i++){
ImageView imageViewByTag = (ImageView) listView.findViewWithTag(urllist.get(i));
SoftReference<Bitmap> softReference = imageCache.get(urllist.get(i));
if (softReference.get() != null) {
imageViewByTag.setImageBitmap( softReference.get());
}
}
for(int i=0;i<getsize;i++){
ImageView imageViewByTag = (ImageView) listView.findViewWithTag(urllist.get(i));
SoftReference<Bitmap> softReference = imageCache.get(urllist.get(i));
if (softReference.get() != null) {
imageViewByTag.setImageBitmap( softReference.get());
}
}