- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的应用程序中有一个 RecyclerView
,其中包含大量图像。当用户使用以下代码滚动 RecyclerView 时,图像被加载:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,url);
else
loader.execute(url);
不幸的是,有时当用户快速滚动时会发生此错误:
Task android.os.AsyncTask$3@73f1d84 rejected from
java.util.concurrent.ThreadPoolExecutor@8f5f96d[Running, pool size = 9,
active threads = 9, queued tasks = 128, completed tasks = 279]
有没有办法检测 poolExecutor 是否已满并跳过图像加载?
整个图像类:
public class Image extends ImageView {
private AsyncTask<String,Integer,Bitmap> loader;
public Image(Context context) {
super(context);
this.setScaleType(ScaleType.FIT_XY);
}
public Image(Context context, AttributeSet attrs) {
super(context, attrs);
this.setScaleType(ScaleType.FIT_XY);
}
public void loadURL(String url) {
if(loader!=null)
loader.cancel(true);
loader=new AsyncTask<String, Integer, Bitmap>() {
@Override
protected Bitmap doInBackground(String... params) {
URL url = null;
byte[] bytes = null;
HttpURLConnection connection=null;
try {
url = new URL(params[0]);
connection=(HttpURLConnection) url.openConnection();
connection.setRequestProperty("Connection", "close");
connection.setRequestMethod("GET");
connection.setUseCaches(true);
InputStream is = null;
is=connection.getInputStream();
bytes = IOUtils.toByteArray(is);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (connection!=null)
connection.disconnect();
Bitmap res=null;
if(!isCancelled() && bytes!=null)
res=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
return res;
}
@Override
protected void onPostExecute(Bitmap res) {
if(res!=null) {
setImageBitmap(res);
_animate();
}
}
};
if (this.getDrawable()!=null) {
Bitmap bmp=((BitmapDrawable) this.getDrawable()).getBitmap();
this.setAnimation(null);
if (bmp!=null) {
bmp.recycle();
//Log.d("image","recycled");
}
this.setImageBitmap(null);
}
/*
ThreadPoolExecutor e =(ThreadPoolExecutor) Executors.newFixedThreadPool(9);
Log.d("pool size",e.getActiveCount()+"/"+e.getMaximumPoolSize());
if (e.getActiveCount() == e.getMaximumPoolSize()) {
}
*/
//start loading
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
else
loader.execute(url);
}
private void _animate() {
ValueAnimator bgAnim= ValueAnimator.ofObject(new IntEvaluator(),0,255);
bgAnim.setDuration(500);
bgAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Image.this.getDrawable().setAlpha((int) (animation.getAnimatedValue()));
}
});
bgAnim.start();
}
最佳答案
我之前回答过这个问题(here、here、here 和 here 可能还有其他),我再次为你回答:不要试图重新发明轮子!
图像加载/缓存在 Android 中是一项非常复杂的任务,许多优秀的开发人员已经做到了这一点。线程只是问题之一,但我可以从您的代码中看到您有内存泄漏,没有缓存,因此如果回滚到它,您将再次重新下载图像,HttpURLConnection
很糟糕网络层。
所以,解决这个问题的方法(恕我直言)就是重复使用其他开发人员完成的工作。您应该考虑的图书馆的好例子是:
Picasso 是我的最爱,所以要使用它你只需调用:
Picasso.with(context).load(url).into(imgView);
这一切都为您处理。
关于安卓:java.util.concurrent.ThreadPoolExecutor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44365214/
我正在尝试在我的项目中使用 Knockout Concurrency 插件,目前我正在摆弄示例代码,但我没有让它工作: https://github.com/AndersMalmgren/Knocko
我正在尝试使用 grunt 运行多个监视任务,但似乎无法运行。我正在使用 grunt concurrent,但它似乎只运行我指定的一部分任务,只是短暂停止。 这是我的 gruntfile 的片段: c
我有一个使用 Grunt 的 Ionic 项目,它是由 Yeoman 构建的。我设法将其配置为在运行 Fedora 22 的本地计算机上正常工作。 目前,我正在尝试在 Centos 7 服务器实例上配
关闭。这个问题需要debugging details .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 1年前关闭。 Improve this question Co
Go is a concurrent lang 这是什么意思? 这是否意味着它是 C/C++/Java.. 的替代品? 最佳答案 A concurrent language是一种具有并发语言结构的语言
我正在尝试使用 Kafka 实现一个事件溯源系统,但遇到了以下问题。在新用户注册期间,我想检查用户提供的用户名是否已被使用。但是,请考虑 2 个用户尝试同时注册提供相同用户名的情况。 根据我对 ES
我正在完成 golang 之旅并进行最后的练习,将网络爬虫更改为并行爬行而不是重复爬行 (http://tour.golang.org/#73)。我只更改了抓取功能。 var used = m
ruby 版本 2.5.3 当我输入 rails new upload_app 时,出现以下错误 错误如下 Traceback (most recent call last): 39: fro
func main() { jobs := []Job{job1, job2, job3} numOfJobs := len(jobs) resultsChan := make
我正在尝试在 Rust async-await(即将稳定)中同时(而不是按顺序)运行 futures 列表,直到它们中的任何一个解析为 true . 想象一下有一个 Vec ,以及为每个文件运行的 f
当我看到这段代码时出现了问题: private static volatile ConcurrentHashMap cMap = null; static { cMap = new Concu
刚在lab环境下安装dcos环境,在centos7 linux机器上尝试安装dcos客户端时得到 **[root@rmavmdock5 dcos]# bash install.sh . http://
为什么要为 Scala fork ForkJoinPool? 哪种实现方式和哪种情况更受欢迎? 最佳答案 scala 库拥有自己的 ForkJoinPool 副本的明显原因是 scala 必须在 1.
是的,我知道。关于 NSOperation 世界有很多问题和答案,但我仍然有一些疑问。我会尝试用两部分的问题来解释我的疑虑。它们相互关联。 在 SO 帖子中 nsoperationqueue-and-
我将 Play Framework 2.1.1 与一个生成 java.util.concurrent.Future 结果的外部 java 库一起使用。我使用的是 scala future 而不是 Ak
我们使用 Doug Lea 的并发库已有 8 年多了。出于向后兼容性的原因,我们的代码仅限于使用 Java 2 语言级别和 JDK 1.3 库。 现在我们正在开发一个主要的新版本,并最终能够使用 Ja
此问题涉及当 saga 数据保留在 Azure 表存储中时对 saga 数据的并发访问。它也是在 Prefer 的文档中找到的引用信息:http://docs.particular.net/nserv
我有一个创建锁的方法。 ReadWriteLock lock = new ReentrantReadWriteLock(); 然后我使用 Lock Interface 将该对象传递到一个方法中。 m
当我在 Mac OSX 命令行上的 python 中执行以下操作时: >>> from concurrent.futures import ProcessPoolExecutor 我明白了 Modul
我正在 listview 的线程池上创建异步任务。我正在通过 asynchtask 的 listarray 处理这些任务。当 fragment 被销毁时我必须删除这些任务,并且当我在销毁最后一个 fr
我是一名优秀的程序员,十分优秀!