日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

并发编程-concurrent指南-线程池ExecutorService的使用

發(fā)布時(shí)間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 并发编程-concurrent指南-线程池ExecutorService的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

有幾種不同的方式來將任務(wù)委托給 ExecutorService 去執(zhí)行:

  • execute(Runnable)
  • submit(Runnable)
  • submit(Callable)
  • invokeAny(…)
  • invokeAll(…)

execute(Runnable)

execute(Runnable) 方法要求一個(gè) java.lang.Runnable 對象,然后對它進(jìn)行異步執(zhí)行。以下是使用 ExecutorService 執(zhí)行一個(gè) Runnable 的示例:

ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(new Runnable() { public void run() { System.out.println("Asynchronous task"); } }); executorService.shutdown();

沒有辦法得知被執(zhí)行的 Runnable 的執(zhí)行結(jié)果。如果有需要的話你得使用一個(gè) Callable(以下將做介紹)。

?

submit(Runnable)

submit(Runnable) 方法也要求一個(gè) Runnable 實(shí)現(xiàn)類,但它返回一個(gè) Future 對象。這個(gè) Future 對象可以用來檢查 Runnable 是否已經(jīng)執(zhí)行完畢。

以下是 ExecutorService submit() 示例:

Future future = executorService.submit(new Runnable() { public void run() { System.out.println("Asynchronous task"); } }); future.get(); //returns null if the task has finished correctly.

?

submit(Callable)

submit(Callable) 方法類似于 submit(Runnable) 方法,除了它所要求的參數(shù)類型之外。Callable 實(shí)例除了它的 call() 方法能夠返回一個(gè)結(jié)果之外和一個(gè) Runnable 很相像。Runnable.run() 不能夠返回一個(gè)結(jié)果。
Callable 的結(jié)果可以通過 submit(Callable) 方法返回的 Future 對象進(jìn)行獲取。以下是一個(gè) ExecutorService Callable 示例:

import java.util.concurrent.*;public class Main {public static void main(String[] args) {ExecutorService executorService = Executors.newSingleThreadExecutor();Future future = executorService.submit(new Runnable() {public void run() {try {TimeUnit.SECONDS.sleep(4);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Asynchronous task");}});System.out.println("主方法。。。。");try {System.out.println(future.get()); //returns null if the task has finished correctly.} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}} }

結(jié)果:

主方法。。。。
Asynchronous task
null

future.get()會(huì)等待子線程結(jié)束后,打印出信息。

submit(Callable)

submit(Callable) 方法類似于 submit(Runnable) 方法,除了它所要求的參數(shù)類型之外。Callable 實(shí)例除了它的 call() 方法能夠返回一個(gè)結(jié)果之外和一個(gè) Runnable 很相像。Runnable.run() 不能夠返回一個(gè)結(jié)果。
Callable 的結(jié)果可以通過 submit(Callable) 方法返回的 Future 對象進(jìn)行獲取。以下是一個(gè) ExecutorService Callable 示例:

import java.util.concurrent.*;public class Main {public static void main(String[] args) {ExecutorService executorService = Executors.newSingleThreadExecutor();Future future = executorService.submit(new Callable(){public Object call() throws Exception {TimeUnit.SECONDS.sleep(3);System.out.println("Asynchronous Callable");return "Callable Result";}});System.out.println("主方法。。。。");try {System.out.println(future.get()); //returns null if the task has finished correctly.} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}} }

結(jié)果:

主方法。。。。
Asynchronous Callable
Callable Result

future.get()會(huì)等待子線程結(jié)束后,打印出信息


invokeAll()

invokeAll() 方法將調(diào)用你在集合中傳給 ExecutorService 的所有 Callable 對象。invokeAll() 返回一系列的 Future 對象,通過它們你可以獲取每個(gè) Callable 的執(zhí)行結(jié)果。

記住,一個(gè)任務(wù)可能會(huì)由于一個(gè)異常而結(jié)束,因此它可能沒有 “成功”。無法通過一個(gè) Future 對象來告知我們是兩種結(jié)束中的哪一種。

以下是一個(gè)代碼示例:

import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.*;public class Main {public static void main(String[] args) {ExecutorService executorService = Executors.newSingleThreadExecutor();Set<Callable<String>> callables = new HashSet<Callable<String>>();callables.add(new Callable<String>() {public String call() throws Exception {System.out.println(Thread.currentThread().getName()+",Task 1");return "Task 1";}});callables.add(new Callable<String>() {public String call() throws Exception {System.out.println(Thread.currentThread().getName()+",Task 2");return "Task 2";}});callables.add(new Callable<String>() {public String call() throws Exception {System.out.println(Thread.currentThread().getName()+",Task 3");return "Task 3";}});callables.add(new Callable<String>() {public String call() throws Exception {System.out.println(Thread.currentThread().getName()+",Task 4");return "Task 4";}});callables.add(new Callable<String>() {public String call() throws Exception {System.out.println(Thread.currentThread().getName()+",Task 5");return "Task 5";}});try {List<Future<String>> futures = executorService.invokeAll(callables);for(Future<String> future : futures){System.out.println("future.get = " + future.get());}} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}executorService.shutdown();} }

?

結(jié)果:

pool-1-thread-1,Task 2
pool-1-thread-1,Task 5
pool-1-thread-1,Task 4
pool-1-thread-1,Task 1
pool-1-thread-1,Task 3
future.get = Task 2
future.get = Task 5
future.get = Task 4
future.get = Task 1
future.get = Task 3

?

ExecutorService 關(guān)閉

使用完 ExecutorService 之后你應(yīng)該將其關(guān)閉,以使其中的線程不再運(yùn)行。

比如,如果你的應(yīng)用是通過一個(gè) main() 方法啟動(dòng)的,之后 main 方法退出了你的應(yīng)用,如果你的應(yīng)用有一個(gè)活動(dòng)的 ExexutorService 它將還會(huì)保持運(yùn)行。ExecutorService 里的活動(dòng)線程阻止了 JVM 的關(guān)閉。

要終止 ExecutorService 里的線程你需要調(diào)用 ExecutorService 的 shutdown() 方法。ExecutorService 并不會(huì)立即關(guān)閉,但它將不再接受新的任務(wù),而且一旦所有線程都完成了當(dāng)前任務(wù)的時(shí)候,ExecutorService 將會(huì)關(guān)閉。在 shutdown() 被調(diào)用之前所有提交給 ExecutorService 的任務(wù)都被執(zhí)行

如果你想要立即關(guān)閉 ExecutorService,你可以調(diào)用 shutdownNow() 方法。這樣會(huì)立即嘗試停止所有執(zhí)行中的任務(wù),并忽略掉那些已提交但尚未開始處理的任務(wù)。無法擔(dān)保執(zhí)行任務(wù)的正確執(zhí)行。可能它們被停止了,也可能已經(jīng)執(zhí)行結(jié)束。

?




?

轉(zhuǎn)載于:https://www.cnblogs.com/qjm201000/p/10156491.html

總結(jié)

以上是生活随笔為你收集整理的并发编程-concurrent指南-线程池ExecutorService的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。