java.util.concurrent.RejectedExecutionException在异步任务的Andr​​oid [英] java.util.concurrent.RejectedExecutionException in Async Task Android

查看:271
本文介绍了java.util.concurrent.RejectedExecutionException在异步任务的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public View getView(final int position, View convertView, ViewGroup parent) {
    SectionHolder sectionHolder = null;
    ViewHolder holder = null;
    convertView = listAdapter.getView(getIndexForPosition(position),
                convertView, parent);
    convertView.setTag(contactsIds[getIndexForPosition(position)]);
    holder = new ViewHolder();
    holder.txtTitle = (TextView) convertView
                .findViewById(R.id.list_item_title);
    holder.id = contactsIds[getIndexForPosition(position)];
    new ThumbnailTask(holder,contactsIds[getIndexForPosition(position)]).execute();
    return convertView;
}

private class ThumbnailTask extends AsyncTask<Void, Void, Integer> {
    private String mId;
    private ViewHolder mHolder;
    public ThumbnailTask(ViewHolder holder, String id) {
        mId = id;
        mHolder = holder;
    }

    @Override
    protected Integer doInBackground(Void... params) {
        // TODO Auto-generated method stub
        int drawableId = getContactStatus(mHolder.id);
        // Log.i("DRAWABLE",drawableId+"");
        return drawableId;
    }

    protected void onPostExecute(Integer drawableId) {
        if (mHolder.id.equals(mId)) {
            if (drawableId != 0) {
                if (UpdateStatusService.user == 1) {
                    mHolder.txtTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,drawableId, 0);
                } else {
                    mHolder.txtTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,R.drawable.ic_action_quetion, 0);
                }
            } else {
                    mHolder.txtTitle.setCompoundDrawablesWithIntrinsicBounds(0,0, android.R.color.transparent, 0);
            }
        }
    }
}
static class ViewHolder {
    public TextView txtTitle;
    public String id;
}

这是我的$ C $适配器C到列表视图的getView。

This is my code of adapter to getView of the listview.

ListView控件包含从Android联系人列表。

ListView Contains the list of contacts from the android.

但我得到的 java.util.concurrent.RejectedExecutionException 异步任务基本上获得图像从服务器和如果有的话号码匹配到服务器的数据,将包含一个图像的设置,否则不

but i am getting the java.util.concurrent.RejectedExecutionException Async task basically get the image from the server and if any number matches to the server data that will contain one image to set otherwise not.

所以,我应该怎么做才能避免这种例外?

So what should i do to avoid this exception?

推荐答案

的AsyncTask 线程被放置在一个工作队列。这项工作队列的限制,你可以有多少线程实例化。如果超过最大,它会给你一个 RejectedExecutionException

AsyncTask threads are placed in a work queue. That work queue restricts how many threads you can instantiate. When you exceed the maximum it will give you a RejectedExecutionException.

解决方案是重构code不实例化线程 getView()或有一个检查,如果一个线程当前行已经开始控制。 getView()被调用非常频繁,所以你最终会超过允许的线程数量,如果你不检查,如果一个线程当前行上运行。

The solution is to refactor your code to not instantiate threads in getView() or have a check that controls if a thread for the current row already has started. getView() gets called very frequently and therefore you will eventually exceed the number of allowed threads if you're not checking if a thread is currently running on a row.

下载链接: https://www.dropbox.com/s/pvr9zyl811tfeem/ListViewImageCache .ZIP

这篇关于java.util.concurrent.RejectedExecutionException在异步任务的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆