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


Java CompletionService.submit方法代码示例

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


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

示例1: 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

示例2: 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

示例3: 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

示例4: 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

示例5: testDoSmth

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
@Test
public void testDoSmth() throws Exception {
    Demo.DemoRequest.Builder req = Demo.DemoRequest.newBuilder();
    req.setUserId(1);

    int multiSize = 12;
    int totalRequestSize = 10;
    ExecutorService pool = Executors.newFixedThreadPool(multiSize);
    CompletionService<Demo.DemoResponse> completionService = new ExecutorCompletionService<Demo.DemoResponse>(
            pool);

    Invoker invoker = new Invoker(req.build());
    long time = System.currentTimeMillis();
    for (int i = 0; i < totalRequestSize; i++) {
        completionService.submit(invoker);
    }

    for (int i = 0; i < totalRequestSize; i++) {
        completionService.take().get();
    }

    long timetook = System.currentTimeMillis() - time;
    System.out.println("Total using " + timetook + "ms");
    System.out.println("QPS:" + 1000f / ((timetook) / (1.0f * totalRequestSize)));
}
 
开发者ID:neoremind,项目名称:navi-pbrpc,代码行数:26,代码来源:SpringIntegrationIpPortStringPooledBlockingIOTest.java

示例6: run

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * 调用服务端
 * 
 * @param port
 * @param multiSize
 *            并发数
 * @param invokeNum
 *            总请求数
 * @param size
 *            batch请求的数据内含的list数量
 * @param textLength
 *            batch请求数据中随机字符串的长度
 * @throws Exception
 */
public void run(int port, int multiSize, int invokeNum, int size, int textLength)
        throws Exception {
    PbrpcClient client = PbrpcClientFactory.buildPooledConnection(new PooledConfiguration(),
            "127.0.0.1", port, 60000);
    ExecutorService pool = Executors.newFixedThreadPool(multiSize);
    CompletionService<DemoBatchResponse> completionService = new ExecutorCompletionService<DemoBatchResponse>(
            pool);

    BatchInvoker invoker = new BatchInvoker(client, size,
            RandomUtils.generateString(textLength));
    long time = System.currentTimeMillis();
    for (int i = 0; i < invokeNum; i++) {
        completionService.submit(invoker);
    }

    for (int i = 0; i < invokeNum; i++) {
        completionService.take().get();
    }

    long timetook = System.currentTimeMillis() - time;
    LOG.info("Send " + invokeNum + " requests using " + timetook + "ms");
    LOG.info("QPS:" + 1000f / ((timetook) / (1.0f * invokeNum)));
}
 
开发者ID:neoremind,项目名称:navi-pbrpc,代码行数:38,代码来源:Client.java

示例7: testPoolBatch

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
public void testPoolBatch() throws Exception {
    PbrpcClient client = PbrpcClientFactory.buildPooledConnection(new PooledConfiguration(),
            "127.0.0.1", 8088, 60000);
    int multiSize = 8;
    int totalRequestSize = 100;
    ExecutorService pool = Executors.newFixedThreadPool(multiSize);
    CompletionService<DemoBatchResponse> completionService = new ExecutorCompletionService<DemoBatchResponse>(
            pool);

    BatchInvoker invoker = new BatchInvoker(client);
    long time = System.currentTimeMillis();
    for (int i = 0; i < totalRequestSize; i++) {
        completionService.submit(invoker);
    }

    for (int i = 0; i < totalRequestSize; i++) {
        completionService.take().get();
    }

    long timetook = System.currentTimeMillis() - time;
    LOG.info("Total using " + timetook + "ms");
    LOG.info("QPS:" + 1000f / ((timetook) / (1.0f * totalRequestSize)));
}
 
开发者ID:neoremind,项目名称:navi-pbrpc,代码行数:24,代码来源:PooledPbrpcClientMainTest.java

示例8: createProjectsParallel

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
private List<String> createProjectsParallel(final Path parentFolder) throws InterruptedException, ExecutionException {
	List<Future<String>> futures = new ArrayList<>();
	CompletionService<String> service = new ExecutorCompletionService<>(Executors.newFixedThreadPool(8));
	List<SpringBootProjectParams> expandedMatrix = generateSringBootMatrix();
	for (final SpringBootProjectParams params : expandedMatrix) {
		service.submit(new Callable<String>() {
			@Override
			public String call() throws Exception {
				return createProject(parentFolder, params);
			}
		});
	}
	for(int i=0 ; i<expandedMatrix.size() ; i++) {
		futures.add(service.take());
	}
	List<String> modules = new ArrayList<>();
	for(Future<String> future : futures) {
		modules.add(future.get());
	}
	return modules;
}
 
开发者ID:groupe-sii,项目名称:ogham,代码行数:22,代码来源:SpringBootProjectRunner.java

示例9: runTest

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
private static void runTest(VehicleController controller, ExecutorService exec, long duration) {
	CompletionService<TaskStatistics> ecs = new ExecutorCompletionService<>(exec);
	List<TaskStatistics> results = new ArrayList<>();

	//submit tasks for concurrent execution
	for (int i = 0; i < NUM_THREADS; i++){
		System.out.println("Submitting task: TASK-" + i);
		ecs.submit(new VehicleTask("TASK-" + i, controller, duration));
	}
	
	// Wait for completion and print individul results
	for (int i = 0; i < NUM_THREADS; ++i) {
		try {
			TaskStatistics nextStat = ecs.take().get();
			results.add(nextStat); //block till next task finishes
			updateCounters(nextStat);
			System.out.println(nextStat); //block till next task finishes
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
	}
	
}
 
开发者ID:iproduct,项目名称:low-latency-high-throughput,代码行数:24,代码来源:GrouperMain.java

示例10: bulkLoad

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * Deserialize {@link Communication} objects in parallel.
 * <br>
 * <br>
 * The {@link ExecutorCompletionService} guarantees that the objects are returned in the order that they are queued.
 * In other words, one can safely iterate over the returned object and wait without truly blocking.
 *
 * @param pathToCommFiles - path to a text file containing paths on disk to serialized {@link Communication} files.
 * @return a {@link List} of {@link Future} objects with a {@link Communication} expected.
 * @throws FileNotFoundException if the passed in {@link Path} does not exist on disk.
 */
public List<Future<Communication>> bulkLoad(Path pathToCommFiles) throws FileNotFoundException {
  List<Path> paths = new ArrayList<>();
  try(Scanner sc = new Scanner(pathToCommFiles.toFile())) {
    while (sc.hasNextLine())
      paths.add(Paths.get(sc.nextLine()));
  }

  CompletionService<Communication> srv = new ExecutorCompletionService<>(this.runner);
  List<Future<Communication>> commList = new ArrayList<>();
  for (Path p : paths) {
    Future<Communication> f = srv.submit(new CallablePathToCommunication(p));
    commList.add(f);
  }

  return commList;
}
 
开发者ID:hltcoe,项目名称:concrete-java,代码行数:28,代码来源:ConcurrentCommunicationLoader.java

示例11: execute

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * GET urls in parallel using the executor service.
 * @param urls absolute URLs to GET
 * @param timeoutMs timeout in milliseconds for each GET request
 * @return instance of CompletionService. Completion service will provide
 *   results as they arrive. The order is NOT same as the order of URLs
 */
public CompletionService<GetMethod> execute(@Nonnull List<String> urls, final int timeoutMs) {
  Preconditions.checkNotNull(urls);
  Preconditions.checkArgument(timeoutMs > 0, "Timeout value for multi-get must be greater than 0");

  CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(executor);
  for (final String url : urls) {
    completionService.submit(new Callable<GetMethod>() {
      @Override
      public GetMethod call()
          throws Exception {
        HttpClient client = new HttpClient(connectionManager);
        GetMethod getMethod = new GetMethod(url);
        getMethod.getParams().setSoTimeout(timeoutMs);
        // if all connections in the connection manager are busy this will wait to retrieve a connection
        // set time to wait to retrieve a connection from connection manager
        client.getParams().setConnectionManagerTimeout(timeoutMs);
        client.executeMethod(getMethod);
        return getMethod;
      }
    });
  }
  return completionService;
}
 
开发者ID:linkedin,项目名称:pinot,代码行数:31,代码来源:MultiGetRequest.java

示例12: submitProcessTask

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
private void submitProcessTask(final CacheLoaderTask<K, V> cacheLoaderTask, CompletionService<Void> ecs,
      final TaskContext taskContext, final Set<Object> batch, final boolean loadEntry, final boolean loadMetadata) {
   ecs.submit(new Callable<Void>() {
      @Override
      public Void call() throws Exception {
         try {
            for (Object key : batch) {
               if (taskContext.isStopped())
                  break;
               if (!loadEntry && !loadMetadata) {
                  cacheLoaderTask.processEntry(
                        initializationContext.getMarshalledEntryFactory().newMarshalledEntry(key, (Object) null,
                              null), taskContext);
               } else {
                  cacheLoaderTask.processEntry(load(key), taskContext);
               }
            }
         } catch (Exception e) {
            log.errorExecutingParallelStoreTask(e);
            throw e;
         }
         return null;
      }
   });
}
 
开发者ID:infinispan,项目名称:infinispan-cachestore-cloud,代码行数:26,代码来源:CloudStore.java

示例13: submitPurgeTask

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
private void submitPurgeTask(CompletionService<Void> ecs, final Set<String> batch, final PurgeListener<? super K> purgeListener) {
   ecs.submit(new Callable<Void>() {
      @Override
      public Void call() throws Exception {
         try {
            for (String key : batch) {
               blobStore.removeBlob(containerName, key);
               purgeListener.entryPurged((K)key2StringMapper.getKeyMapping(key));
            }
         } catch (Exception e) {
            log.errorExecutingParallelStoreTask(e);
            throw e;
         }
         return null;
      }
   });
}
 
开发者ID:infinispan,项目名称:infinispan-cachestore-cloud,代码行数:18,代码来源:CloudStore.java

示例14: JohnsenJohanssonLattice

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
public JohnsenJohanssonLattice(SubcontextList sublist) throws InterruptedException, ExecutionException {
    // first organize sub labels by outcome for quick H(p) construction
    Map<Double, List<Label>> outcomeSubMap = new HashMap<>();
    for (Subcontext s : sublist) {
        List<Label> l = outcomeSubMap.computeIfAbsent(s.getOutcome(), k -> new ArrayList<>());
        l.add(s.getLabel());
    }
    // Estimate the counts for each supracontext in parallel
    ExecutorService executor = Executors.newFixedThreadPool(NUM_CORES);
    CompletionService<Supracontext> taskCompletionService = new ExecutorCompletionService<>(executor);
    for (Subcontext p : sublist) {
        taskCompletionService.submit(new SupraApproximator(p, outcomeSubMap));
    }
    for (int i = 0; i < sublist.size(); i++) {
        supras.add(taskCompletionService.take().get());
    }
    executor.shutdownNow();
}
 
开发者ID:garfieldnate,项目名称:Weka_AnalogicalModeling,代码行数:19,代码来源:JohnsenJohanssonLattice.java

示例15: test

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
    double wucha = 0.05;
    int count = 1000;
    int wuchaNum = (int) (count * wucha);
    int high = count + wuchaNum;
    int low = count - wuchaNum;
    System.err.println("共有" + count + "个数参与测试,误差系数为" + wucha + "误差值为" + wuchaNum);

    ExecutorService executorService = Executors.newFixedThreadPool(20);
    final List<Long> results = new ArrayList<>();
    CompletionService<Long> cs = new ExecutorCompletionService<>(executorService);
    for (int i = 1; i < count; i++) {
        cs.submit(() -> {
            Thread.sleep(RandomUtils.nextInt(1, 2000));
            return IdWorker.getId();
        });
    }
    for (int i = 0; i < count; i++) {
        Future<Long> future = executorService.submit(IdWorker::getId);
        results.add(future.get());
    }
    executorService.shutdown();
    HashSet<Long> set = new HashSet<>(results);
    // 判断是否有重复
    Assert.assertEquals(count, set.size());
    int odd = 0;
    int even = 0;
    for (Long id : results) {
        if (id % 2 != 0) {
            odd++;
        } else {
            even++;
        }
    }
    System.err.println("奇数:" + odd);
    System.err.println("偶数:" + even);
    Assert.assertTrue(odd >= low && odd <= high);
    Assert.assertTrue(even >= low && even <= high);
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:41,代码来源:IdWorkerTest.java


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