java.util.concurrent.RejectedExecutionException 与线程池执行器
java.util.concurrent.RejectedExecutionException with Thread Pool Executor
我在适配器中调用 executeOnExecutor()
时收到 RejectedExecutionException,但这仅在这种情况下发生:
该应用程序在具有非常大屏幕的设备上运行,例如 Nexus 10,而不是较小的设备,并且仅在 class 中屏幕处于纵向(而非横向)模式的情况下,其中适配器被调用。
这是我的适配器代码:
public class HomeListAdapter extends ArrayAdapter<Manga> {
Gson gson;
private final Context context;
private List<Manga> list;
public HomeListAdapter(Context context, List<Manga> list) {
super(context, R.layout.homelistadapter, list);
this.context = context;
this.list = list;
gson = new Gson();
}
static class ViewHolder{
TextView tvTitle;
TextView tvChapter;
TextView tvDate;
ImageView immagine;
int position;
}
@Override
public View getView(final int position, View rowView, ViewGroup parent) {
final ViewHolder viewHolder;
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.homelistadapter, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvTitle = (TextView) rowView.findViewById(R.id.tvTitle);
viewHolder.tvChapter = (TextView) rowView.findViewById(R.id.tvChapter);
viewHolder.tvDate = (TextView) rowView.findViewById(R.id.tvDate);
viewHolder.immagine = (ImageView) rowView.findViewById(R.id.imageView);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.position = position;
//HERE is where i'm getting the exception only on largest screens
new AsyncList(viewHolder, position).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "url");
return rowView;
}
private class AsyncList extends AsyncTask<String, Void, MangaSpec> {
private ViewHolder holder;
private int position;
public AsyncList(ViewHolder viewholder, int position){
this.holder = viewholder;
this.position = position;
}
@Override
protected MangaSpec doInBackground(String... params) {
String urlManga = null;
try {
urlManga = MainActivity.connessione(params[0]);
} catch (Exception e) {
e.printStackTrace();
}
assert urlManga != null;
return gson.fromJson(urlManga.trim(), MangaSpec.class);
}
@Override
protected void onPostExecute(MangaSpec manga) {
super.onPostExecute(manga);
if(holder.position == position){
holder.tvTitle.setText(list.get(position).getT());
holder.tvTitle.setSelected(true);
if(Integer.parseInt(String.valueOf(list.get(position).getS()))==2)
holder.immagine.setImageResource(R.drawable.book_close);
List generic = manga.getChapters();
List chapters = (List) generic.get(0);
double numero = (Double) chapters.get(0);
String titoloC = (String) chapters.get(2);
if((numero-(int)numero)!=0)
holder.tvChapter.setText(numero+" - "+titoloC);
else
holder.tvChapter.setText((int)numero+" - "+titoloC);
holder.tvChapter.setSelected(true);
double a = ((Number) chapters.get(1)).doubleValue();
Date date = new Date((long) (a*1000));
String formattedDate = new SimpleDateFormat("HH:mm - dd/MM/yy").format(date);
holder.tvDate.setText(formattedDate);
}
}
}
}
这是我的 LogCat:
java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask@331ef9b7 rejected from java.util.concurrent.ThreadPoolExecutor@ed04524[Running, pool size = 9, active threads = 9, queued tasks = 128, completed tasks = 245]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:590)
at com.example.giorgio.mangaproject.HomeListAdapter.getView(HomeListAdapter.java:61)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
您启动的 AsyncTask
个实例过多。在该设备上,使用四核 CPU,一次最多 9 个任务 运行,并且工作队列提供额外的 128 个插槽。您已尝试并行生成第 138 个项目。
修改您的代码以使用更少的并发 AsyncTask
个实例。或者,使用您自己的自定义 Executor
和 executeOnExecutor()
。
我在适配器中调用 executeOnExecutor()
时收到 RejectedExecutionException,但这仅在这种情况下发生:
该应用程序在具有非常大屏幕的设备上运行,例如 Nexus 10,而不是较小的设备,并且仅在 class 中屏幕处于纵向(而非横向)模式的情况下,其中适配器被调用。
这是我的适配器代码:
public class HomeListAdapter extends ArrayAdapter<Manga> {
Gson gson;
private final Context context;
private List<Manga> list;
public HomeListAdapter(Context context, List<Manga> list) {
super(context, R.layout.homelistadapter, list);
this.context = context;
this.list = list;
gson = new Gson();
}
static class ViewHolder{
TextView tvTitle;
TextView tvChapter;
TextView tvDate;
ImageView immagine;
int position;
}
@Override
public View getView(final int position, View rowView, ViewGroup parent) {
final ViewHolder viewHolder;
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.homelistadapter, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvTitle = (TextView) rowView.findViewById(R.id.tvTitle);
viewHolder.tvChapter = (TextView) rowView.findViewById(R.id.tvChapter);
viewHolder.tvDate = (TextView) rowView.findViewById(R.id.tvDate);
viewHolder.immagine = (ImageView) rowView.findViewById(R.id.imageView);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.position = position;
//HERE is where i'm getting the exception only on largest screens
new AsyncList(viewHolder, position).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "url");
return rowView;
}
private class AsyncList extends AsyncTask<String, Void, MangaSpec> {
private ViewHolder holder;
private int position;
public AsyncList(ViewHolder viewholder, int position){
this.holder = viewholder;
this.position = position;
}
@Override
protected MangaSpec doInBackground(String... params) {
String urlManga = null;
try {
urlManga = MainActivity.connessione(params[0]);
} catch (Exception e) {
e.printStackTrace();
}
assert urlManga != null;
return gson.fromJson(urlManga.trim(), MangaSpec.class);
}
@Override
protected void onPostExecute(MangaSpec manga) {
super.onPostExecute(manga);
if(holder.position == position){
holder.tvTitle.setText(list.get(position).getT());
holder.tvTitle.setSelected(true);
if(Integer.parseInt(String.valueOf(list.get(position).getS()))==2)
holder.immagine.setImageResource(R.drawable.book_close);
List generic = manga.getChapters();
List chapters = (List) generic.get(0);
double numero = (Double) chapters.get(0);
String titoloC = (String) chapters.get(2);
if((numero-(int)numero)!=0)
holder.tvChapter.setText(numero+" - "+titoloC);
else
holder.tvChapter.setText((int)numero+" - "+titoloC);
holder.tvChapter.setSelected(true);
double a = ((Number) chapters.get(1)).doubleValue();
Date date = new Date((long) (a*1000));
String formattedDate = new SimpleDateFormat("HH:mm - dd/MM/yy").format(date);
holder.tvDate.setText(formattedDate);
}
}
}
}
这是我的 LogCat:
java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask@331ef9b7 rejected from java.util.concurrent.ThreadPoolExecutor@ed04524[Running, pool size = 9, active threads = 9, queued tasks = 128, completed tasks = 245]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:590)
at com.example.giorgio.mangaproject.HomeListAdapter.getView(HomeListAdapter.java:61)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
您启动的 AsyncTask
个实例过多。在该设备上,使用四核 CPU,一次最多 9 个任务 运行,并且工作队列提供额外的 128 个插槽。您已尝试并行生成第 138 个项目。
修改您的代码以使用更少的并发 AsyncTask
个实例。或者,使用您自己的自定义 Executor
和 executeOnExecutor()
。