我使用异步任务每2秒刷新一次ListView。为此,我每隔2秒使用ScheduledExecutorService执行相同的任务。但我得到了这个有线问题..RejectedExecutionException ..我也提到了像
这样的帖子RejectedExecutionException from AsyncTask, but haven't hit limits, Avoiding RejectedExecutionException in Android 4.4 when app uses list
没有得到解决方案。请帮忙 .. Thnx提前:))
错误堆栈:
java.util.concurrent.RejectedExecutionException: Task com.stripe.android.compat.AsyncTask$3@23e7c86b rejected from java.util.concurrent.ThreadPoolExecutor@16831fc8[Running, pool size = 1, active threads = 1, queued tasks = 10, completed tasks = 3]
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 com.stripe.android.compat.AsyncTask.executeOnExecutor(AsyncTask.java:536)
at com.stripe.android.compat.AsyncTask.execute(AsyncTask.java:481)
at com.cabbooking.passengerapp.screens.BookACabFragment$3$1.run(BookACabFragment.java:492)
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:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我在onResume中启动我的调度程序,如下所示:
public void startTimeScheduledExecutorService(){
Toast.makeText(getActivity(), "start scheduler", Toast.LENGTH_SHORT).show();
this.startGetCabs="startGetCabs";
scheduler = Executors.newScheduledThreadPool(1);
scheduledFuture = scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
AsyncTask.class.getMethod("setDefaultExecutor", Executor.class).invoke(null, AsyncTask.SERIAL_EXECUTOR);
GetOnlineDriversNoBar g = new GetOnlineDriversNoBar();
g.execute();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
});
//
}
},0, 2000, TimeUnit.MILLISECONDS);
}
我正在onPause和onStop上停止我的调度程序:
public void stopTimeScheduledExecutorService() {
if(startGetCabs.equalsIgnoreCase("startGetCabs")){
if (!scheduledFuture.isCancelled()) {
scheduledFuture.cancel(true);
// scheduler.shutdown();
Toast.makeText(getActivity(), "shutdown scheduler", Toast.LENGTH_SHORT).show();
// timer = null;
}}
}