當前位置: 首頁>>代碼示例>>Java>>正文


Java CompletableFuture.thenApplyAsync方法代碼示例

本文整理匯總了Java中java.util.concurrent.CompletableFuture.thenApplyAsync方法的典型用法代碼示例。如果您正苦於以下問題:Java CompletableFuture.thenApplyAsync方法的具體用法?Java CompletableFuture.thenApplyAsync怎麽用?Java CompletableFuture.thenApplyAsync使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.CompletableFuture的用法示例。


在下文中一共展示了CompletableFuture.thenApplyAsync方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getResponseAsync

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
CompletableFuture<Response> getResponseAsync(Executor executor) {
    CompletableFuture<Response> cf = pushCF.whenComplete((v, t) -> pushGroup.pushError(t));
    if(executor!=null && !cf.isDone()) {
        cf  = cf.thenApplyAsync( r -> r, executor);
    }
    return cf;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:Stream.java

示例2: mapConstN

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public String mapConstN() throws InterruptedException, ExecutionException {
  CompletableFuture<String> f = constFuture;
  for (int i = 0; i < N.n; i++)
    f = f.thenApplyAsync(mapF);
  return f.get();
}
 
開發者ID:traneio,項目名稱:future,代碼行數:8,代碼來源:JavaAsyncFutureBenchmark.java

示例3: mapPromise

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public String mapPromise() throws InterruptedException, ExecutionException {
  CompletableFuture<String> p = new CompletableFuture<String>();
  CompletableFuture<String> f = p.thenApplyAsync(mapF);
  p.complete(string);
  return f.get();
}
 
開發者ID:traneio,項目名稱:future,代碼行數:8,代碼來源:JavaAsyncFutureBenchmark.java

示例4: mapPromiseN

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public String mapPromiseN() throws InterruptedException, ExecutionException {
  CompletableFuture<String> p = new CompletableFuture<String>();
  CompletableFuture<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.thenApplyAsync(mapF);
  p.complete(string);
  return f.get();
}
 
開發者ID:traneio,項目名稱:future,代碼行數:10,代碼來源:JavaAsyncFutureBenchmark.java

示例5: setValueN

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public String setValueN() throws InterruptedException, ExecutionException {
  CompletableFuture<String> p = new CompletableFuture<>();
  CompletableFuture<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.thenApplyAsync(mapF);
  p.complete(string);
  return f.get();
}
 
開發者ID:traneio,項目名稱:future,代碼行數:10,代碼來源:JavaAsyncFutureBenchmark.java

示例6: fireClickEvent

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
private void fireClickEvent(ClickInventoryEvent e)
{
    Optional<Player> optional = Sponge.getServer().getPlayer(playerUniqueId);
    if (!optional.isPresent())
    {
        return;
    }
    Player player = optional.get();
    CompletableFuture<Boolean> future = CompletableFuture.completedFuture(Boolean.TRUE);
    for (SlotTransaction slotTransaction : e.getTransactions())
    {
        Slot slot = slotTransaction.getSlot();
        SlotIndex pos = SpongeUnimplemented.getSlotOrdinal(slot);
        if (SpongeUnimplemented.isSlotInInventory(slot, e.getTargetInventory()) && slotToListen.matches(pos))
        {
            e.setCancelled(true);
            boolean allowAction = actionIntervalManager.allowAction(player, acceptableActionIntervalTick);
            if (allowAction && itemsInSlots.containsKey(pos))
            {
                future = future.thenApplyAsync(p -> processClickEvent(e, player, pos) && p, executorService);
            }
        }
    }
    future.thenAccept(shouldKeepInventoryOpen ->
    {
        if (!shouldKeepInventoryOpen)
        {
            SpongeUnimplemented.closeInventory(player, plugin);
        }
    });
}
 
開發者ID:ustc-zzzz,項目名稱:VirtualChest,代碼行數:32,代碼來源:VirtualChestInventory.java

示例7: main

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public static void main(String[] args) {

		System.out.printf("Main: Start\n");
		CompletableFuture<Integer> seedFuture = new CompletableFuture<>();
		Thread seedThread = new Thread(new SeedGenerator(seedFuture));
		seedThread.start();

		System.out.printf("Main: Getting the seed\n");
		int seed = 0;
		try {
			seed = seedFuture.get();
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
		System.out.printf("Main: The seed is: %d\n", seed);

		System.out.printf("Main: Launching the list of numbers generator\n");
		NumberListGenerator task = new NumberListGenerator(seed);
		CompletableFuture<List<Long>> startFuture = CompletableFuture.supplyAsync(task);

		System.out.printf("Main: Launching step 1\n");
		CompletableFuture<Long> step1Future = startFuture.thenApplyAsync(list -> {
			System.out.printf("%s: Step 1: Start\n", Thread.currentThread().getName());
			long selected = 0;
			long selectedDistance = Long.MAX_VALUE;
			long distance;
			for (Long number : list) {
				distance = Math.abs(number - 1000);
				if (distance < selectedDistance) {
					selected = number;
					selectedDistance = distance;
				}
			}
			System.out.printf("%s: Step 1: Result - %d\n", Thread.currentThread().getName(), selected);
			return selected;
		});

		System.out.printf("Main: Launching step 2\n");
		CompletableFuture<Long> step2Future = startFuture
				.thenApplyAsync(list -> list.stream().max(Long::compare).get());

		CompletableFuture<Void> write2Future = step2Future.thenAccept(selected -> {
			System.out.printf("%s: Step 2: Result - %d\n", Thread.currentThread().getName(), selected);
		});

		System.out.printf("Main: Launching step 3\n");
		NumberSelector numberSelector = new NumberSelector();
		CompletableFuture<Long> step3Future = startFuture.thenApplyAsync(numberSelector);

		System.out.printf("Main: Waiting for the end of the three steps\n");
		CompletableFuture<Void> waitFuture = CompletableFuture.allOf(step1Future, write2Future, step3Future);
		CompletableFuture<Void> finalFuture = waitFuture.thenAcceptAsync((param) -> {
			System.out.printf("Main: The CompletableFuture example has been completed.");
		});

		finalFuture.join();

	}
 
開發者ID:PacktPublishing,項目名稱:Java-9-Concurrency-Cookbook-Second-Edition,代碼行數:59,代碼來源:Main.java

示例8:

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public <T,U> CompletableFuture<U> thenApply
    (CompletableFuture<T> f, Function<? super T,U> a) {
    return f.thenApplyAsync(a);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:5,代碼來源:CompletableFutureTest.java

示例9: ThreadExecutor

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public <T,U> CompletableFuture<U> thenApply
    (CompletableFuture<T> f, Function<? super T,U> a) {
    return f.thenApplyAsync(a, new ThreadExecutor());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:5,代碼來源:CompletableFutureTest.java


注:本文中的java.util.concurrent.CompletableFuture.thenApplyAsync方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。