ThreadPoolExecutor在关闭后抛出RejectedExecutionException

时间:2013-11-18 03:21:36

标签: android multithreading threadpoolexecutor

我有两个按钮用于开始下载和停止下载。我使用ThreadPoolExecutor进行多次下载。单击停止按钮时关闭ThreadPoolExecutor。当单击开始按钮时,我会在ThreadPoolExecutor上再次开始执行。我得到了RejectedExecutionException。 我想知道如何重新执行已经关闭的ThreadPoolExecutor

修改 错误堆栈。

11-18 10:41:52.929: E/AndroidRuntime(27351): FATAL EXCEPTION: main
11-18 10:41:52.929: E/AndroidRuntime(27351): java.util.concurrent.RejectedExecutionException: Task com.task.SyncTask@42614978 rejected from java.util.concurrent.ThreadPoolExecutor@414ce138[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 181]
11-18 10:41:52.929: E/AndroidRuntime(27351):    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1979)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:786)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1307)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at com.MainActivity$2.handleMessage(MainActivity.java:394)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at android.os.Looper.loop(Looper.java:213)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at android.app.ActivityThread.main(ActivityThread.java:5092)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at java.lang.reflect.Method.invoke(Method.java:511)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
11-18 10:41:52.929: E/AndroidRuntime(27351):    at dalvik.system.NativeStart.main(Native Method)

感谢。

1 个答案:

答案 0 :(得分:1)

根据the docs,在调用关机后“不会接受任何新任务”。

您不需要显式关闭执行程序(一旦完成其中的所有线程,它就会自行处理)。

你得到的RejectedExecutionException中是否有消息?