如何终止 android线程池中的任务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何终止 android线程池中的任务相关的知识,希望对你有一定的参考价值。
终止android线程池中的任务的方法
1.实现Callable接口
2.调用pool.submit()方法,返回 futrue对象
3.用 future对象来获取线程的状态。
- void test()
ExecutorService pool = Executors.newFixedThreadPool(2);
Callable<String> s = new Callable<String>()
@Override
public String call() throws Exception
System.out.println("test");
return "true";
;
Future<String> f = pool.submit( s);
System.out.println(f.isCancelled());
System.out.println(f.isDone());
f.cancel(true);
可以关闭 ExecutorService,这将导致其拒绝新任务。提供两个方法来关闭 ExecutorService。shutdown() 方法在终止前允许执行以前提交的任务,而 shutdownNow() 方法阻止等待任务启动并试图停止当前正在执行的任务。在终止时,执行程序没有任务在执行,也没有任务在等待执行,并且无法提交新任务。应该关闭未使用的 ExecutorService 以允许回收其资源。
通过创建并返回一个可用于取消执行和/或等待完成的 Future,方法 submit 扩展了基本方法 Executor.execute(java.lang.Runnable)。方法 invokeAny 和 invokeAll 是批量执行的最常用形式,它们执行任务 collection,然后等待至少一个,或全部任务完成(可使用 ExecutorCompletionService 类来编写这些方法的自定义变体)。
Executors 类提供了用于此包中所提供的执行程序服务的工厂方法。本回答被提问者采纳
Java并发程序设计线程池之异常终止和正常关闭
1.1. 线程池中的线程的异常终止
如果线程池中的线程的任务代码发生异常导致线程终止,线程池会自动创建一个新线程。
对于各种类型的线程池,都是如此。以下代码在单个线程的线程池中抛出一个异常,可以发现后续任务中输出的每个tid的值都不相同。
ExecutorService executorService = Executors.newSingleThreadExecutor(); for(int j=0;j<10;j++){ final int t = j; executorService.execute( new Runnable(){ @Override public void run() { for(int i=0;i<4;i++){ System.out.println("t:" + t + "," + "i:" + i + ", tid:" + Thread.currentThread().getId()); int a = 3 /0;//产生/ by zero异常,线程终止。新任务将创建新线程。 } } }); }
输出信息中可以看到对每个任务(t),线程id(tid)都不同。
t:0,i:0, tid:8
t:1,i:0, tid:10
Exception in thread "pool-1-thread-1" t:2,i:0, tid:11
Exception in thread "pool-1-thread-2" Exception in thread "pool-1-thread-3" java.lang.ArithmeticException: / by zero
t:3,i:0, tid:12
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "pool-1-thread-4" java.lang.ArithmeticException: / by zero
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
t:4,i:0, tid:13
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.ArithmeticException: / by zero
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
t:5,i:0, tid:14
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.ArithmeticException: / by zero
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
t:6,i:0, tid:15
Exception in thread "pool-1-thread-6" Exception in thread "pool-1-thread-5" Exception in thread "pool-1-thread-7" java.lang.ArithmeticException: / by zero
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
t:7,i:0, tid:16
at java.lang.Thread.run(Unknown Source)
java.lang.ArithmeticException: / by zero
t:8,i:0, tid:17 at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "pool-1-thread-9" Exception in thread "pool-1-thread-8" java.lang.ArithmeticException: / by zero
t:9,i:0, tid:18 at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "pool-1-thread-10" java.lang.ArithmeticException: / by zero
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.ArithmeticException: / by zero
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.ArithmeticException: / by zero
at com.test.concurrence.ThreadPoolTest$3.run(ThreadPoolTest.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
1.2. 线程池的终止
shutdownNow()和shutdown()都可以终止线程池,两者都可以终止线程池,阻止新任务的提交。
shutdownNow()会停止当前正在执行的任务,清除已提交但是尚未运行的任务,然后终止线程池。
shutdown()会等待已经提交的所有任务执行完毕才终止线程池。
ExecutorService executorService = Executors.newFixedThreadPool(2); for(int j=0;j<10;j++){ final int t = j; executorService.execute( new Runnable(){ @Override public void run() { for(int i=0;i<4;i++){ String s = "t:" + t + "," + "i:" + i + ", tid:" + Thread.currentThread().getId(); System.out.println(s); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); } executorService.shutdownNow();
t:0,i:0, tid:8
end.
t:1,i:0, tid:9
java.lang.InterruptedException: sleep interrupted
t:0,i:1, tid:8
at java.lang.Thread.sleep(Native Method)
at com.test.concurrence.ThreadPoolTest$1.run(ThreadPoolTest.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.test.concurrence.ThreadPoolTest$1.run(ThreadPoolTest.java:27)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
t:1,i:1, tid:9
t:0,i:2, tid:8
t:1,i:2, tid:9
t:0,i:3, tid:8
t:1,i:3, tid:9
以上是关于如何终止 android线程池中的任务的主要内容,如果未能解决你的问题,请参考以下文章