当前位置: 首页>>代码示例>>Java>>正文


Java CompletionService类代码示例

本文整理汇总了Java中java.util.concurrent.CompletionService的典型用法代码示例。如果您正苦于以下问题:Java CompletionService类的具体用法?Java CompletionService怎么用?Java CompletionService使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


CompletionService类属于java.util.concurrent包,在下文中一共展示了CompletionService类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testContainerLocalizerClosesFilesystems

import java.util.concurrent.CompletionService; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked") // mocked generics
public void testContainerLocalizerClosesFilesystems() throws Exception {
  // verify filesystems are closed when localizer doesn't fail
  FileContext fs = FileContext.getLocalFSFileContext();
  spylfs = spy(fs.getDefaultFileSystem());
  ContainerLocalizer localizer = setupContainerLocalizerForTest();
  doNothing().when(localizer).localizeFiles(any(LocalizationProtocol.class),
      any(CompletionService.class), any(UserGroupInformation.class));
  verify(localizer, never()).closeFileSystems(
      any(UserGroupInformation.class));
  localizer.runLocalization(nmAddr);
  verify(localizer).closeFileSystems(any(UserGroupInformation.class));

  spylfs = spy(fs.getDefaultFileSystem());
  // verify filesystems are closed when localizer fails
  localizer = setupContainerLocalizerForTest();
  doThrow(new YarnRuntimeException("Forced Failure")).when(localizer).localizeFiles(
      any(LocalizationProtocol.class), any(CompletionService.class),
      any(UserGroupInformation.class));
  verify(localizer, never()).closeFileSystems(
      any(UserGroupInformation.class));
  localizer.runLocalization(nmAddr);
  verify(localizer).closeFileSystems(any(UserGroupInformation.class));
}
 
开发者ID:naver,项目名称:hadoop,代码行数:26,代码来源:TestContainerLocalizer.java

示例2: solveAny

import java.util.concurrent.CompletionService; //导入依赖的package包/类
void solveAny(Executor e,
              Collection<Callable<Integer>> solvers)
    throws InterruptedException {
    CompletionService<Integer> cs
        = new ExecutorCompletionService<>(e);
    int n = solvers.size();
    List<Future<Integer>> futures = new ArrayList<>(n);
    Integer result = null;
    try {
        solvers.forEach(solver -> futures.add(cs.submit(solver)));
        for (int i = n; i > 0; i--) {
            try {
                Integer r = cs.take().get();
                if (r != null) {
                    result = r;
                    break;
                }
            } catch (ExecutionException ignore) {}
        }
    } finally {
        futures.forEach(future -> future.cancel(true));
    }

    if (result != null)
        use(result);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:ExecutorCompletionService9Test.java

示例3: testPoll1

import java.util.concurrent.CompletionService; //导入依赖的package包/类
/**
 * poll returns non-null when the returned task is completed
 */
public void testPoll1()
    throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    assertNull(cs.poll());
    cs.submit(new StringTask());

    long startTime = System.nanoTime();
    Future f;
    while ((f = cs.poll()) == null) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:20,代码来源:ExecutorCompletionServiceTest.java

示例4: testPoll2

import java.util.concurrent.CompletionService; //导入依赖的package包/类
/**
 * timed poll returns non-null when the returned task is completed
 */
public void testPoll2()
    throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    assertNull(cs.poll());
    cs.submit(new StringTask());

    long startTime = System.nanoTime();
    Future f;
    while ((f = cs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:20,代码来源:ExecutorCompletionServiceTest.java

示例5: testPollReturnsNull

import java.util.concurrent.CompletionService; //导入依赖的package包/类
/**
 * poll returns null before the returned task is completed
 */
public void testPollReturnsNull()
    throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    final CountDownLatch proceed = new CountDownLatch(1);
    cs.submit(new Callable() { public String call() throws Exception {
        proceed.await();
        return TEST_STRING;
    }});
    assertNull(cs.poll());
    assertNull(cs.poll(0L, MILLISECONDS));
    assertNull(cs.poll(Long.MIN_VALUE, MILLISECONDS));
    long startTime = System.nanoTime();
    assertNull(cs.poll(timeoutMillis(), MILLISECONDS));
    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
    proceed.countDown();
    assertSame(TEST_STRING, cs.take().get());
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:21,代码来源:ExecutorCompletionServiceTest.java

示例6: testTaskAssortment

import java.util.concurrent.CompletionService; //导入依赖的package包/类
/**
 * successful and failed tasks are both returned
 */
public void testTaskAssortment()
    throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    ArithmeticException ex = new ArithmeticException();
    for (int i = 0; i < 2; i++) {
        cs.submit(new StringTask());
        cs.submit(callableThrowing(ex));
        cs.submit(runnableThrowing(ex), null);
    }
    int normalCompletions = 0;
    int exceptionalCompletions = 0;
    for (int i = 0; i < 3 * 2; i++) {
        try {
            if (cs.take().get() == TEST_STRING)
                normalCompletions++;
        }
        catch (ExecutionException expected) {
            assertTrue(expected.getCause() instanceof ArithmeticException);
            exceptionalCompletions++;
        }
    }
    assertEquals(2 * 1, normalCompletions);
    assertEquals(2 * 2, exceptionalCompletions);
    assertNull(cs.poll());
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:29,代码来源:ExecutorCompletionServiceTest.java

示例7: solveAny

import java.util.concurrent.CompletionService; //导入依赖的package包/类
void solveAny(Executor e,
              Collection<Callable<Integer>> solvers)
    throws InterruptedException {
    CompletionService<Integer> cs
        = new ExecutorCompletionService<>(e);
    int n = solvers.size();
    List<Future<Integer>> futures = new ArrayList<>(n);
    Integer result = null;
    try {
        solvers.forEach((solver) -> futures.add(cs.submit(solver)));
        for (int i = n; i > 0; i--) {
            try {
                Integer r = cs.take().get();
                if (r != null) {
                    result = r;
                    break;
                }
            } catch (ExecutionException ignore) {}
        }
    } finally {
        futures.forEach((future) -> future.cancel(true));
    }

    if (result != null)
        use(result);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:27,代码来源:ExecutorCompletionService9Test.java

示例8: getFirstToComplete

import java.util.concurrent.CompletionService; //导入依赖的package包/类
private ByteBuffer getFirstToComplete(
    CompletionService<ByteBuffer> hedgedService,
    ArrayList<Future<ByteBuffer>> futures) throws InterruptedException {
  if (futures.isEmpty()) {
    throw new InterruptedException("let's retry");
  }
  Future<ByteBuffer> future = null;
  try {
    future = hedgedService.take();
    ByteBuffer bb = future.get();
    futures.remove(future);
    return bb;
  } catch (ExecutionException | CancellationException e) {
    // already logged in the Callable
    futures.remove(future);
  }

  throw new InterruptedException("let's retry");
}
 
开发者ID:aliyun-beta,项目名称:aliyun-oss-hadoop-fs,代码行数:20,代码来源:DFSInputStream.java

示例9: main

import java.util.concurrent.CompletionService; //导入依赖的package包/类
public static void main(String []args) throws InterruptedException, ExecutionException {
	final Random random = new Random();
	ExecutorService executorService = Executors.newFixedThreadPool(10);
	CompletionService<String>completionService = new ExecutorCompletionService<String>(executorService);
	for(int i = 0 ; i < 100 ; i++) {
		final int num = i;
		completionService.submit(new Callable<String>() {
			public String call() {
				try {
					Thread.sleep((random.nextLong()) & 5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				return "num" + num;
			}
		});
	}
	for(int i = 0 ; i < 100 ; i++) {
		Future<String> f = completionService.take();
		System.out.println(f.get());
	}
	executorService.shutdown();
}
 
开发者ID:hdcuican,项目名称:java_learn,代码行数:24,代码来源:CompletionServiceTest.java

示例10: exec

import java.util.concurrent.CompletionService; //导入依赖的package包/类
/**
 * 根据指定的列表关键数据及列表数据处理器,并发地处理并返回处理后的列表数据集合
 * @param allKeys 列表关键数据
 * @param handleBizDataFunc 列表数据处理器
 * @param <T> 待处理的数据参数类型
 * @param <R> 待返回的数据结果类型
 * @return 处理后的列表数据集合
 *
 * NOTE: 类似实现了 stream.par.map 的功能,不带延迟计算
 */
public static <T,R> List<R> exec(List<T> allKeys, Function<List<T>, List<R>> handleBizDataFunc) {
  List<String> parts = TaskUtil.divide(allKeys.size(), TASK_SIZE);
  //System.out.println(parts);

  CompletionService<List<R>>
      completionService = new ExecutorCompletionService<>(executor);

  ForeachUtil.foreachDone(parts, (part) -> {
    final List<T> tmpRowkeyList = TaskUtil.getSubList(allKeys, part);
    completionService.submit(
        () -> handleBizDataFunc.apply(tmpRowkeyList));  // lambda replace inner class
  });

  // foreach code refining
  List<R> result = ForeachUtil.foreachAddWithReturn(parts.size(), (ind) -> get(ind, completionService));
  return result;
}
 
开发者ID:shuqin,项目名称:ALLIN,代码行数:28,代码来源:ExecutorUtil.java

示例11: finalizeQueue

import java.util.concurrent.CompletionService; //导入依赖的package包/类
/**
 * Removes all task from queue and shuts down executor service.
 *
 * @param taskQueue         Queue of tasks
 * @param completionService Producer of new asynchronous tasks
 * @param executorService   Tasks executor service
 */
public void finalizeQueue(Queue<Future<Void>> taskQueue,
    CompletionService<Void> completionService, ExecutorService executorService) {
  try {
    while (!taskQueue.isEmpty()) {
      taskQueue.remove(completionService.take());
    }
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
  } finally {
    while (!taskQueue.isEmpty()) {
      taskQueue.poll().cancel(true);
    }
    executorService.shutdownNow();
  }
}
 
开发者ID:Cognifide,项目名称:bobcat,代码行数:23,代码来源:QueueFinalizer.java

示例12: getFirstToComplete

import java.util.concurrent.CompletionService; //导入依赖的package包/类
private ByteBuffer getFirstToComplete(
    CompletionService<ByteBuffer> hedgedService,
    ArrayList<Future<ByteBuffer>> futures) throws InterruptedException {
  if (futures.isEmpty()) {
    throw new InterruptedException("let's retry");
  }
  Future<ByteBuffer> future = null;
  try {
    future = hedgedService.take();
    ByteBuffer bb = future.get();
    futures.remove(future);
    return bb;
  } catch (ExecutionException e) {
    // already logged in the Callable
    futures.remove(future);
  } catch (CancellationException ce) {
    // already logged in the Callable
    futures.remove(future);
  }

  throw new InterruptedException("let's retry");
}
 
开发者ID:yncxcw,项目名称:big-c,代码行数:23,代码来源:DFSInputStream.java

示例13: addToRequestQueue

import java.util.concurrent.CompletionService; //导入依赖的package包/类
public Boolean addToRequestQueue(JRTServerConfigRequest request, boolean forceResponse, CompletionService<Boolean> completionService) {
    // It's no longer delayed if we get here
    request.setDelayedResponse(false);
    //ConfigDebug.logDebug(log, System.currentTimeMillis(), request.getConfigKey(), "RpcServer.addToRequestQueue()");
    try {
        final GetConfigProcessor task = new GetConfigProcessor(this, request, forceResponse);
        if (completionService == null) {
            executorService.submit(task);
        } else {
            completionService.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    task.run();
                    return true;
                }
            });
        }
        updateWorkQueueMetrics();
        return true;
    } catch (RejectedExecutionException e) {
        request.addErrorResponse(ErrorCode.INTERNAL_ERROR, "getConfig request queue size is larger than configured max limit");
        respond(request);
        return false;
    }
}
 
开发者ID:vespa-engine,项目名称:vespa,代码行数:26,代码来源:RpcServer.java

示例14: trainEachWithEarlyUpdate

import java.util.concurrent.CompletionService; //导入依赖的package包/类
private void trainEachWithEarlyUpdate(Sentence sentence, CompletionService<List<BeamItem>> completionService) {
    State.StateIterator iterator = oracle.getState(sentence).getIterator();
    State oracleState = iterator.next(); // initial state
    List<BeamItem> beam = new ArrayList<>(1);
    beam.add(new BeamItem(new State(sentence), 0.0));

    boolean terminate = false;
    while (!terminate) {
        oracleState = iterator.next();
        beam = getNextBeamItems(beam, beamWidth, classifier, completionService);
        terminate = beam.stream().allMatch(item -> item.getState().isTerminal());

        final State finalOracleState = oracleState; // make a variable final to use it in lambda
        beam.stream().forEach(item -> {
            if (item.getState().equals(finalOracleState)) {
                System.out.println("pred.hashCode: " + item.getState().hashCode() + ", oracle.hashCode: " + finalOracleState.hashCode());
            }
        });
        boolean oracleInBeam = beam.stream().anyMatch(item -> item.getState().equals(finalOracleState));;
        if (!oracleInBeam || (!terminate && !iterator.hasNext())) {
            classifier.update(oracleState, beam.get(0).getState()); // early update
            break;
        }
    }
}
 
开发者ID:chantera,项目名称:teraparser,代码行数:26,代码来源:BeamSearchDecoderTest.java

示例15: getNextBeamItems

import java.util.concurrent.CompletionService; //导入依赖的package包/类
public List<BeamItem> getNextBeamItems(List<BeamItem> beam, int beamWidth, Perceptron classifier, CompletionService<List<BeamItem>> completionService) {
    try {
        List<BeamItem> items1 = BeamSearchDecoder.super.getNextBeamItems(beam, beamWidth, classifier);
        List<BeamItem> items2 = BeamSearchDecoder.super.getNextBeamItems(beam, beamWidth, classifier, completionService);
        for (int i = 0; i < items1.size(); i++) {
            BeamItem item1 = items1.get(i);
            BeamItem item2 = items2.get(i);
            System.out.println("item1: " + item1.getState() + ": score=" + item1.getScore());
            System.out.println("item2: " + item2.getState() + ": score=" + item2.getScore());
            if (!item1.equals(item2)) {
                throw new Exception(item1 + " != " + item2);
            }
        }
        System.out.println("========");
        return items2;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:chantera,项目名称:teraparser,代码行数:20,代码来源:BeamSearchDecoderTest.java


注:本文中的java.util.concurrent.CompletionService类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。