成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android自定義線程池的編程實(shí)戰(zhàn)

移動(dòng)開(kāi)發(fā) Android
Executor框架便是Java 5中引入的,其內(nèi)部使用了線程池機(jī)制,它在java.util.cocurrent 包下,通過(guò)該框架來(lái)控制線程的啟動(dòng)、執(zhí)行和關(guān)閉,可以簡(jiǎn)化并發(fā)編程的操作。

1、Executor 簡(jiǎn)介

在Java 5之后,并發(fā)編程引入了一堆新的啟動(dòng)、調(diào)度和管理線程的API。Executor框架便是Java 5中引入的,其內(nèi)部使用了線程池機(jī)制,它在java.util.cocurrent 包下,通過(guò)該框架來(lái)控制線程的啟動(dòng)、執(zhí)行和關(guān)閉,可以簡(jiǎn)化并發(fā)編程的操作。因此,在Java 5之后,通過(guò)Executor來(lái)啟動(dòng)線程比使用Thread的start方法更好,除了更易管理,效率更好(用線程池實(shí)現(xiàn),節(jié)約開(kāi)銷)外,還有關(guān)鍵的一點(diǎn):有助于避免this逃逸問(wèn)題——如果我們?cè)跇?gòu)造器中啟動(dòng)一個(gè)線程,因?yàn)榱硪粋€(gè)任務(wù)可能會(huì)在構(gòu)造器結(jié)束之前開(kāi)始執(zhí)行,此時(shí)可能會(huì)訪問(wèn)到初始化了一半的對(duì)象用Executor在構(gòu)造器中。

Executor框架包括:線程池,Executor,Executors,ExecutorService,CompletionService,F(xiàn)uture,Callable等。

在java代碼中 Executor是一個(gè)接口,只有一個(gè)方法。

  1. public interface Executor { 
  2.   
  3.     /** 
  4.      * Executes the given command at some time in the future.  The command 
  5.      * may execute in a new thread, in a pooled thread, or in the calling 
  6.      * thread, at the discretion of the {@code Executor} implementation. 
  7.      * 
  8.      * @param command the runnable task 
  9.      * @throws RejectedExecutionException if this task cannot be 
  10.      * accepted for execution 
  11.      * @throws NullPointerException if command is null 
  12.      */ 
  13.     void execute(Runnable command); 

2、ExecutorService

ExecutorService 是一個(gè)接口,繼承 Executor ,除了有execute( Runnable command) 方法外,還拓展其他的方法:

  1. public interface ExecutorService extends Executor { 
  2.  
  • void shutdown();
  • List<Runnable> shutdownNow();
  • boolean isShutdown();
  • boolean isTerminated();
  • boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
  • <T> Future<T> submit(Callable<T> task); //提交一個(gè)任務(wù)
  • <T> Future<T> submit(Runnable task, T result); //提交一個(gè)任務(wù)
  • Future<?> submit(Runnable task); //提交一個(gè)任務(wù)
  • <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;
  • <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException;
  • <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException;
  • <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;

2.1 execute(Runnable)

接收一個(gè) java.lang.Runnable 對(duì)象作為參數(shù),并且以異步的方式執(zhí)行它。如下是一個(gè)使用 ExecutorService 執(zhí)行 Runnable 的例子

  1. package com.app; 
  2.  
  3. import java.util.concurrent.ExecutorService; 
  4.  
  5. import java.util.concurrent.Executors; 
  6.  
  7. public class ExecutorTest { 
  8.  
  9. public static void main(String[] args) { 
  10.  
  11. //創(chuàng)建一個(gè)線程數(shù)固定大小為10的線程池 
  12.  
  13. ExecutorService executorService = Executors.newFixedThreadPool( 10 ) ; 
  14.  
  15. //執(zhí)行一個(gè)任務(wù) 該任務(wù)是 new Runnable() 對(duì)象 
  16.  
  17. executorService.execute( new Runnable() { 
  18.  
  19. @Override 
  20.  
  21. public void run() { 
  22.  
  23. Log.d( Thread.currentThread().getName() ); 
  24.  
  25.  
  26. }); 
  27.  
  28. //關(guān)閉線程池 
  29.  
  30. executorService.shutdown(); 
  31.  
  32.  

結(jié)果:

  1. pool-1-thread-1 

使用這種方式?jīng)]有辦法獲取執(zhí)行 Runnable 之后的結(jié)果,如果你希望獲取運(yùn)行之后的返回值,就必須使用 接收 Callable 參數(shù)的 execute() 方法,后者將會(huì)在下文中提到。

2.2、submit(Runnable)

方法 submit(Runnable) 同樣接收一個(gè) Runnable 的實(shí)現(xiàn)作為參數(shù),但是會(huì)返回一個(gè) Future 對(duì)象。這個(gè) Future 對(duì)象可以用于判斷 Runnable 是否結(jié)束執(zhí)行。如下是一個(gè) ExecutorService 的 submit() 方法的例子:

  1. package com.app; 
  2.  
  3. import java.util.concurrent.ExecutorService; 
  4.  
  5. import java.util.concurrent.Executors; 
  6.  
  7. import java.util.concurrent.Future; 
  8.  
  9. public class ExecutorTest { 
  10.  
  11. public static void main(String[] args) { 
  12.  
  13. //創(chuàng)建一個(gè)線程數(shù)固定大小為10的線程池 
  14.  
  15. ExecutorService executorService = Executors.newFixedThreadPool( 10 ) ; 
  16.  
  17. //執(zhí)行一個(gè)任務(wù) 該任務(wù)是 new Runnable() 對(duì)象 
  18.  
  19. Future future = executorService.submit( new Runnable() { 
  20.  
  21. @Override 
  22.  
  23. public void run() { 
  24.  
  25. Log.d( Thread.currentThread().getName() ); 
  26.  
  27.  
  28. }); 
  29.  
  30. try { 
  31.  
  32. //如果任務(wù)結(jié)束執(zhí)行則返回 null 
  33.  
  34. Log.d( ""+ future.get() ); 
  35.  
  36. } catch (Exception e) { 
  37.  
  38. e.printStackTrace(); 
  39.  
  40.  
  41. //關(guān)閉線程池 
  42.  
  43. executorService.shutdown(); 
  44.  
  45.  

結(jié)果:

  1. pool-1-thread-1 
  2.  
  3. null 

2.3 submit(Callable)

方法 submit(Callable) 和方法 submit(Runnable) 比較類似,但是區(qū)別則在于它們接收不同的參數(shù)類型。Callable 的實(shí)例與 Runnable 的實(shí)例很類似,但是 Callable 的 call() 方法可以返回壹個(gè)結(jié)果。方法 Runnable.run() 則不能返回結(jié)果。

Callable 的返回值可以從方法 submit(Callable) 返回的 Future 對(duì)象中獲取。如下是一個(gè) ExecutorService Callable 的例子:

  1. package com.app; 
  2.  
  3. import java.util.concurrent.Callable; 
  4.  
  5. import java.util.concurrent.ExecutorService; 
  6.  
  7. import java.util.concurrent.Executors; 
  8.  
  9. import java.util.concurrent.Future; 
  10.  
  11. public class ExecutorTest { 
  12.  
  13. public static void main(String[] args) { 
  14.  
  15. //創(chuàng)建一個(gè)線程數(shù)固定大小為10的線程池 
  16.  
  17. ExecutorService executorService = Executors.newFixedThreadPool( 10 ) ; 
  18.  
  19. //執(zhí)行一個(gè)任務(wù) 該任務(wù)是 new Callable() 對(duì)象 
  20.  
  21. Future future = executorService.submit( new Callable<String>() { 
  22.  
  23. @Override 
  24.  
  25. public String call() throws Exception { 
  26.  
  27. return "執(zhí)行完了" ; 
  28.  
  29.  
  30. }) ; 
  31.  
  32. try { 
  33.  
  34. //如果任務(wù)結(jié)束執(zhí)行則返回 
  35.  
  36. Log.d( "結(jié)果是: "+ future.get() ); 
  37.  
  38. } catch (Exception e) { 
  39.  
  40. e.printStackTrace(); 
  41.  
  42.  
  43. //關(guān)閉線程池 
  44.  
  45. executorService.shutdown(); 
  46.  
  47.  

結(jié)果:

結(jié)果是: 執(zhí)行完了

2.4、inVokeAny()

方法 invokeAny() 接收一個(gè)包含 Callable 對(duì)象的集合作為參數(shù)。調(diào)用該方法不會(huì)返回 Future 對(duì)象,而是返回集合中某一個(gè) Callable 對(duì)象的結(jié)果,而且無(wú)法保證調(diào)用之后返回的結(jié)果是哪一個(gè) Callable,只知道它是這些 Callable 中一個(gè)執(zhí)行結(jié)束的 Callable 對(duì)象。如果一個(gè)任務(wù)運(yùn)行完畢或者拋出異常,方法會(huì)取消其它的 Callable 的執(zhí)行。

  1. package com.app; 
  2.  
  3. import java.util.ArrayList; 
  4.  
  5. import java.util.List; 
  6.  
  7. import java.util.concurrent.Callable; 
  8.  
  9. import java.util.concurrent.ExecutionException; 
  10.  
  11. import java.util.concurrent.ExecutorService; 
  12.  
  13. import java.util.concurrent.Executors; 
  14.  
  15. public class ExecutorTest { 
  16.  
  17. public static void main(String[] args) { 
  18.  
  19. //創(chuàng)建一個(gè)線程數(shù)固定大小為10的線程池 
  20.  
  21. ExecutorService executorService = Executors.newFixedThreadPool( 10 ) ; 
  22.  
  23. List<Callable<String>> list = new ArrayList<>() ; 
  24.  
  25. //創(chuàng)建***個(gè) Callable 
  26.  
  27. Callable<String> callable1 = new Callable<String>() { 
  28.  
  29. @Override 
  30.  
  31. public String call() throws Exception { 
  32.  
  33. Log.d( "callable 1 線程是: "+ Thread.currentThread().getName() ); 
  34.  
  35. return "執(zhí)行完了 callable 1" ; 
  36.  
  37.  
  38. }; 
  39.  
  40. //創(chuàng)建第二個(gè) Callable 
  41.  
  42. Callable<String> callable2 = new Callable<String>() { 
  43.  
  44. @Override 
  45.  
  46. public String call() throws Exception { 
  47.  
  48. Log.d( "callable 2 線程是: "+ Thread.currentThread().getName() ); 
  49.  
  50. return "執(zhí)行完了 callable 2" ; 
  51.  
  52.  
  53. }; 
  54.  
  55. list.add( callable1 ) ; 
  56.  
  57. list.add( callable2 ) ; 
  58.  
  59. try { 
  60.  
  61. String result = executorService.invokeAny( list ) ; 
  62.  
  63. Log.d( "結(jié)果是: "+ result ); 
  64.  
  65. } catch (InterruptedException e1) { 
  66.  
  67. e1.printStackTrace(); 
  68.  
  69. } catch (ExecutionException e1) { 
  70.  
  71. e1.printStackTrace(); 
  72.  
  73.  
  74. //關(guān)閉線程池 
  75.  
  76. executorService.shutdown(); 
  77.  
  78.  

結(jié)果:

callable 1 線程是: pool-1-thread-1

callable 2 線程是: pool-1-thread-2

結(jié)果是: 執(zhí)行完了 callable 2

總結(jié):

1、可以看到 Callable 里面的call方法,都是在子線程中運(yùn)行的,

2、 executorService.invokeAny( list ) ;返回值是任意一個(gè) Callable 的返回值 。具體是哪一個(gè),每個(gè)都有可能。

2.5、invokeAll()

方法 invokeAll() 會(huì)調(diào)用存在于參數(shù)集合中的所有 Callable 對(duì)象,并且返回一個(gè)包含 Future 對(duì)象的集合,你可以通過(guò)這個(gè)返回的集合來(lái)管理每個(gè) Callable 的執(zhí)行結(jié)果。需要注意的是,任務(wù)有可能因?yàn)楫惓6鴮?dǎo)致運(yùn)行結(jié)束,所以它可能并不是真的成功運(yùn)行了。但是我們沒(méi)有辦法通過(guò) Future 對(duì)象來(lái)了解到這個(gè)差異。

  1. package com.app; 
  2.  
  3. import java.util.ArrayList; 
  4.  
  5. import java.util.List; 
  6.  
  7. import java.util.concurrent.Callable; 
  8.  
  9. import java.util.concurrent.ExecutorService; 
  10.  
  11. import java.util.concurrent.Executors; 
  12.  
  13. import java.util.concurrent.Future; 
  14.  
  15. public class ExecutorTest { 
  16.  
  17. public static void main(String[] args) { 
  18.  
  19. //創(chuàng)建一個(gè)線程數(shù)固定大小為10的線程池 
  20.  
  21. ExecutorService executorService = Executors.newFixedThreadPool( 10 ) ; 
  22.  
  23. List<Callable<String>> list = new ArrayList<>() ; 
  24.  
  25. //創(chuàng)建***個(gè) Callable 
  26.  
  27. Callable<String> callable1 = new Callable<String>() { 
  28.  
  29. @Override 
  30.  
  31. public String call() throws Exception { 
  32.  
  33. Log.d( "callable 1 線程是: "+ Thread.currentThread().getName() ); 
  34.  
  35. return "執(zhí)行完了 callable 1" ; 
  36.  
  37.  
  38. }; 
  39.  
  40. //創(chuàng)建第二個(gè) Callable 
  41.  
  42. Callable<String> callable2 = new Callable<String>() { 
  43.  
  44. @Override 
  45.  
  46. public String call() throws Exception { 
  47.  
  48. Log.d( "callable 2 線程是: "+ Thread.currentThread().getName() ); 
  49.  
  50. return "執(zhí)行完了 callable 2" ; 
  51.  
  52.  
  53. }; 
  54.  
  55. list.add( callable1 ) ; 
  56.  
  57. list.add( callable2 ) ; 
  58.  
  59. List<Future<String>> result; 
  60.  
  61. try { 
  62.  
  63. result = executorService.invokeAll( list ); 
  64.  
  65. for (Future<String> future : result) { 
  66.  
  67. Log.d( "結(jié)果是: "+ future.get() ); 
  68.  
  69.  
  70. } catch (Exception e) { 
  71.  
  72. e.printStackTrace(); 
  73.  
  74.  
  75. //關(guān)閉線程池 
  76.  
  77. executorService.shutdown(); 
  78.  
  79.  

結(jié)果

callable 1 線程是: pool-1-thread-1

callable 2 線程是: pool-1-thread-2

結(jié)果是: 執(zhí)行完了 callable 1

結(jié)果是: 執(zhí)行完了 callable 2

注意:1:Callable 的call方法都是執(zhí)行在子線程中的

2: executorService.invokeAll( list ) 是返回值。 但是必須是所有的 Callable對(duì)象執(zhí)行完了,才會(huì)返回,返回值是一個(gè)list, 順序和 List<Callable>一樣 。在執(zhí)行的過(guò)程中,如果任何一個(gè)Callable發(fā)生異常,程序會(huì)崩潰,沒(méi)有返回值。

2.6 如何關(guān)閉 ExecuteService 服務(wù) ?

當(dāng)使用 ExecutorService 完畢之后,我們應(yīng)該關(guān)閉它,這樣才能保證線程不會(huì)繼續(xù)保持運(yùn)行狀態(tài)。 舉例來(lái)說(shuō),如果你的程序通過(guò) main() 方法啟動(dòng),并且主線程退出了你的程序,如果你還有一個(gè)活動(dòng)的 ExecutorService 存在于你的程序中,那么程序?qū)?huì)繼續(xù)保持運(yùn)行狀態(tài)。存在于 ExecutorService 中的活動(dòng)線程會(huì)阻Java虛擬機(jī)關(guān)閉。

為了關(guān)閉在 ExecutorService 中的線程,你需要調(diào)用 shutdown() 方法。ExecutorService 并不會(huì)馬上關(guān)閉,而是不再接收新的任務(wù),一旦所有的線程結(jié)束執(zhí)行當(dāng)前任務(wù),ExecutorServie 才會(huì)真的關(guān)閉。所有在調(diào)用 shutdown() 方法之前提交到 ExecutorService 的任務(wù)都會(huì)執(zhí)行。

如果你希望立即關(guān)閉 ExecutorService,你可以調(diào)用 shutdownNow() 方法。這個(gè)方法會(huì)嘗試馬上關(guān)閉所有正在執(zhí)行的任務(wù),并且跳過(guò)所有已經(jīng)提交但是還沒(méi)有運(yùn)行的任務(wù)。但是對(duì)于正在執(zhí)行的任務(wù),是否能夠成功關(guān)閉它是無(wú)法保證 的,有可能他們真的被關(guān)閉掉了,也有可能它會(huì)一直執(zhí)行到任務(wù)結(jié)束。這是一個(gè)***的嘗試。

責(zé)任編輯:武曉燕 來(lái)源: 博客園
相關(guān)推薦

2010-03-18 16:19:02

Java自定義線程池

2016-11-16 21:55:55

源碼分析自定義view androi

2018-06-21 14:46:03

Spring Boot異步調(diào)用

2016-12-26 15:25:59

Android自定義View

2022-06-24 07:08:24

OHOS自定義服務(wù)

2014-12-10 10:37:45

Android自定義布局

2013-04-01 14:35:10

Android開(kāi)發(fā)Android自定義x

2010-02-07 14:02:16

Android 界面

2017-05-18 12:36:16

android萬(wàn)能適配器列表視圖

2017-05-19 10:03:31

AndroidBaseAdapter實(shí)踐

2016-04-12 10:07:55

AndroidViewList

2024-10-25 08:30:57

計(jì)算機(jī)視覺(jué)神經(jīng)網(wǎng)絡(luò)YOLOv8模型

2023-06-07 13:49:00

多線程編程C#

2013-05-02 14:08:18

2015-02-11 17:49:35

Android源碼自定義控件

2015-02-12 15:33:43

微信SDK

2025-07-03 07:10:00

線程池并發(fā)編程代碼

2013-03-28 10:58:30

自定義Android界android

2013-01-09 17:22:38

Android開(kāi)發(fā)Camera

2015-01-14 15:06:48

定義相機(jī)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产精品久久久久婷婷二区次 | 99精品国产一区二区三区 | www日本在线播放 | 成人精品一区二区 | 国产精品不卡 | 午夜久久久久久久久久一区二区 | 久久1区 | 精品亚洲一区二区 | 欧美日韩国产一区二区三区 | 自拍偷拍亚洲视频 | 国产精品一区二区三区久久久 | 在线视频一区二区三区 | 国产一区二区三区 | 亚洲激情一区二区三区 | 婷婷福利视频导航 | 精品欧美一区二区三区久久久 | 天天操夜夜操 | 深夜爽视频 | 在线一区 | 在线视频99 | 亚洲精品免费观看 | 综合色播 | 亚洲视频一区在线观看 | 91免费小视频 | 日韩欧美电影在线 | 成人欧美一区二区 | 国产精品不卡 | 日本黄色免费视频 | 国产精品久久久亚洲 | 日韩欧美在线视频播放 | 最新日韩在线视频 | 99久久国产 | 亚洲欧美精品 | 国产三级一区二区 | 欧美精品一区在线发布 | 九九伊人sl水蜜桃色推荐 | 久久久久国产视频 | 九九成人 | 日韩福利在线观看 | 国产一区二区三区四区区 | 99精品久久久 |