第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

“主”線程的 Java 執(zhí)行器

“主”線程的 Java 執(zhí)行器

月關(guān)寶盒 2023-05-17 17:49:17
我想Executor從程序的&ldquo;主&rdquo;線程(類似于 Android 中的主循環(huán)程序)創(chuàng)建一個(gè),然后運(yùn)行直到它處理完提交給它的所有內(nèi)容:public class MyApp {? private static Callable<Integer> task = () -> {? ? // ... return an int somehow ...? };? public static void main(String[] args) throws ExecutionException, InterruptedException {? ? ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));? ? Thread main = Thread.currentThread();? ? ExecutorService executorService = Executors.newSingleThreadExecutor(r -> main);? ? service.submit(task).addListener(() -> {? ? ? /// ... do something with the result ...? ? }, executorService);? ? executorService.awaitTermination(100, TimeUnit.SECONDS);? }}但我得到一個(gè)IllegalThreadState例外:SEVERE: RuntimeException while executing runnable MyApp$$Lambda$20/0x00000008000a6440@71f06a3c with executor java.util.concurrent.Executors$FinalizableDelegatedExecutorService@47add263java.lang.IllegalThreadStateException? ? at java.base/java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:926)? ? at java.base/java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1343)? ? at java.base/java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:687)? ? at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1137)? ? at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:957)? ? at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:726)? ??我可以在一個(gè)新線程上開始一個(gè)ExecutorService線程,然后等待它,但這似乎很浪費(fèi)。有沒(méi)有一種從當(dāng)前線程創(chuàng)建一個(gè)Executor并等待它處理已提交給它的所有內(nèi)容的好方法?
查看完整描述

4 回答

?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用 Guava 的MoreExecutors.newDirectExecutorService()

這將確保提交的代碼將在 ThreadPool 的同一線程中執(zhí)行。我知道,這不是主線程,但至少您不會(huì)像您想要的那樣只為偵聽(tīng)器創(chuàng)建其他新線程。

import com.google.common.util.concurrent.*;

import org.junit.jupiter.api.Test;

import java.util.concurrent.*;


class ExecutorTest {

? private static Callable<Integer> task = () -> {

? ? System.out.println("in call: " + Thread.currentThread().getName());

? ? TimeUnit.SECONDS.sleep(1);

? ? return 0;

? };

? @Test

? void test() throws InterruptedException {

? ? ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));

? ? ExecutorService executor = MoreExecutors.newDirectExecutorService();

? ? service.submit(task).addListener(() -> {

? ? ? System.out.println("in listener: " + Thread.currentThread().getName());

? ? }, executor);

? ? executor.awaitTermination(2, TimeUnit.SECONDS);

? }

}


查看完整回答
反對(duì) 回復(fù) 2023-05-17
?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

您正在嘗試在主線程中執(zhí)行回調(diào)。直接的方法是調(diào)用get()

YourResult?result?=?service.submit(task).get();
/*?Do?something?with?your?result...?*/

如果您想異步執(zhí)行回調(diào),您可能會(huì)對(duì)CompletionStage

CompletableFuture.runAsync(task).thenAccept(r?->?/*?do?something?with?result?*/);



查看完整回答
反對(duì) 回復(fù) 2023-05-17
?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

此處invokeAll()記錄的方法?等待所有任務(wù)完成?您可以這樣做

executorService.invokeAll();


查看完整回答
反對(duì) 回復(fù) 2023-05-17
?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可能正在尋找類似的東西CompletionService:您向其提交任務(wù),它會(huì)按完成順序返回給您。

ExecutorService service = Executors.newFixedThreadPool(1);

ExecutorCompletionService<Integer> comp = new ExecutorCompletionService<>(service);


comp.submit(task);

// ... I assume you want to submit N tasks.


for (int i = 0; i < N; ++i) {

? Future<Integer> future = comp.take();

? Integer result = future.get();


? // ... do something with the result ...

}

這將在項(xiàng)目完成后立即對(duì)其進(jìn)行處理。如果你能等到你擁有一切,然后一次性處理它,你可以使用番石榴Futures.allAsList


ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));

List<Callable<Integer>> tasks = ...;


List<ListenableFuture<Integer>> futures = new ArrayList<>();

for (Callable<Integer> task : tasks) {

? futures.add(service.submit(task));

}


ListenableFuture<List<Integer>> resultsFuture = Futures.allAsList(futures);

List<Integer> results = resultsFuture.get();


// Process the results.


查看完整回答
反對(duì) 回復(fù) 2023-05-17
  • 4 回答
  • 0 關(guān)注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)