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

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

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

Task.blocking介绍

[英]This method provides a way to create an asynchronous task from a blocking or long running callables like JDBC requests. Unlike with tasks created with all other methods a callable passed as a parameter is not executed on ParSeq's thread but instead it is executed on specified Executor. It means that callable does not get any special memory consistency guarantees and should not attempt to use shared state.

In order to avoid creating tasks that never complete the Executor passed in as a parameter must signal execution rejection by throwing an exception.

In order to prevent blocking ParSeq threads the Executor passed in as a parameter must not use ThreadPoolExecutor.CallerRunsPolicyas a rejection policy.
[中]此方法提供了一种从阻塞或长时间运行的可调用项(如JDBC请求)创建异步任务的方法。与使用所有其他方法创建的任务不同,作为参数传递的可调用任务不会在ParSeq的线程上执行,而是在指定的执行器上执行。这意味着callable不会得到任何特殊的内存一致性保证,也不应该尝试使用共享状态。
为了避免创建永远无法完成的任务,作为参数传入的执行器必须通过抛出异常来发出执行拒绝的信号。
为了防止阻塞ParSeq线程,作为参数传入的执行器不能使用ThreadPoolExecutor。CallerRunPolicy作为拒绝策略。

代码示例

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

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

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

@Override
 public Task<Map<Integer, Try<String>>> taskForBatch(Set<Integer> keys) {
  return Task.blocking(() -> {
   try {
    // make this batching task long-running
    Thread.sleep(_sleepMs);
   } catch (InterruptedException ignored) {
   }
   return keys.stream().collect(Collectors.toMap(k -> k, k -> Success.of(Integer.toString(k))));
  }, _executorService);
 }
}

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

@Test
public void testBlocking() {
 TestingExecutorService es = new TestingExecutorService(Executors.newSingleThreadExecutor());
 try {
  Task<String> task = Task.blocking(() -> "from blocking", es);
  runAndWait("TestTaskFactoryMethods.testBlocking", task);
  assertEquals(task.get(), "from blocking");
  assertEquals(es.getCount(), 1);
  assertEquals(countTasks(task.getTrace()), 1);
 } finally {
  es.shutdown();
 }
}

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

@Test
public void testBlockingTaskType() {
 TestingExecutorService es = new TestingExecutorService(Executors.newSingleThreadExecutor());
 try {
  Task<String> task = Task.blocking(() -> "blocking task", es);
  runAndWait("blockingTaskType", task);
  assertEquals(task.getShallowTrace().getTaskType(), TaskType.BLOCKING.getName());
 } finally {
  es.shutdown();
 }
}

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

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

代码示例来源:origin: com.linkedin.pegasus/restli-int-test-server

@Override
 public Task<Void> get(String key, final @UnstructuredDataWriterParam UnstructuredDataWriter writer)
 {
  return Task.blocking("fetchBytes", () ->
  {
   GreetingUnstructuredDataUtils.respondGoodUnstructuredData(writer);
   return null;
  }, _scheduler);
 }
}

相关文章