com.linkedin.parseq.Task.fromCompletionStage()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(3.5k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中com.linkedin.parseq.Task.fromCompletionStage()方法的一些代码示例,展示了Task.fromCompletionStage()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Task.fromCompletionStage()方法的具体详情如下:
包路径:com.linkedin.parseq.Task
类名称:Task
方法名:fromCompletionStage

Task.fromCompletionStage介绍

[英]Creates a new task that's value will be set to the value returned from the CompletionStage. Returned task will fail if callable passed in as a parameter throws an exception.
[中]创建一个新任务,该任务的值将设置为CompletionStage返回的值。如果作为参数传入的callable引发异常,则返回的任务将失败。

代码示例

代码示例来源:origin: linkedin/parseq

/**
 * Equivalent to {@code fromCompletionStage("fromCompletionStage", callable)}.
 * @see #fromCompletionStage(Callable) (String, Callable)
 */
public static <T> Task<T> fromCompletionStage(final Callable<CompletionStage<? extends T>> callable) {
 return fromCompletionStage(
   "fromCompletionStage: " + _taskDescriptor.getDescription(callable.getClass().getName()), callable);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testFromCompletionStageWithCallableException() {
 Task<String> task = Task.fromCompletionStage(() ->{
  throw new RuntimeException();
 });
 runAndWaitException("testFromCompletionStageWithCallableException", task, RuntimeException.class);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testFromCompletionStageWithCompletionStageException() {
 Task<String> task = Task.fromCompletionStage(() ->{
  CompletableFuture<String> completableFuture
    = CompletableFuture.supplyAsync(() -> {
   throw new RuntimeException();
  });
  return completableFuture;
 });
 runAndWaitException("testFromCompletionStageWithCompletionStageException", task, CompletionException.class);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testFromCompletionStage() {
 String result = "FromCompletionStageResult";
 Task<String> task = Task.fromCompletionStage(() ->{
  CompletableFuture<String> completableFuture
    = CompletableFuture.supplyAsync(() -> result);
  return completableFuture;
 });
 runAndWait("testFromCompletionStage", task);
 assertEquals(result, task.get());
}

代码示例来源:origin: linkedin/parseq

@Test
public void testFromCompletionStageWithTimeConsumingFuture() throws InterruptedException {
 String result = "FromCompletionStageResult";
 Task<String> task = Task.fromCompletionStage(() ->{
  CompletableFuture<String> completableFuture
    = CompletableFuture.supplyAsync(() -> {
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    throw new RuntimeException(e);
   }
   return result;
  });
  return completableFuture;
 });
 runAndWait("testFromCompletionStageWithTimeConsumingFuture", task);
 assertEquals(result, task.get());
}

代码示例来源:origin: linkedin/parseq

@Test
public void testToCompletionStage() throws InterruptedException, ExecutionException {
 String result = "CompletionStageResult";
 Task<String> task = Task.fromCompletionStage(() ->{
  CompletableFuture<String> completableFuture
    = CompletableFuture.supplyAsync(() -> result);
  return completableFuture;
 });
 CompletionStage<String> future = task.toCompletionStage();
 runAndWait("TestTasks.testToCompletionStage", task);
 future.whenComplete((r, ex) -> assertEquals(result, r));
}

代码示例来源:origin: com.linkedin.parseq/parseq

/**
 * Equivalent to {@code fromCompletionStage("fromCompletionStage", callable)}.
 * @see #fromCompletionStage(Callable) (String, Callable)
 */
public static <T> Task<T> fromCompletionStage(final Callable<CompletionStage<? extends T>> callable) {
 return fromCompletionStage(
   "fromCompletionStage: " + _taskDescriptor.getDescription(callable.getClass().getName()), callable);
}

相关文章