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


Java CompletionService.take方法代码示例

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


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

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

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

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

示例4: execute

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
@Override
@NonNull
public Pair<Boolean,Collection<? extends URL>> execute(
        @NonNull final Function<URL,Boolean> fnc,
        @NonNull final Callable<Boolean> cancel,
        @NonNull final Collection<? extends URL> binaries) {
    final CompletionService<URL> cs = new ExecutorCompletionService<URL>(RP);
    int submitted = 0;
    for (URL binary : binaries) {
        cs.submit(new Task(binary,fnc, cancel));
        submitted++;
    }
    final Collection<URL> result = new ArrayDeque<URL>();
    //Don't break the cycle when is canceled,
    //rather wait for all submitted task, they should die fast.
    //The break will cause logging of wrong number of scanned roots.
    for (int i=0; i< submitted; i++) {
        try {                    
            final Future<URL> becomeURL = cs.take();
            final URL url = becomeURL.get();
            if (url != null) {
                result.add(url);
            }
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    boolean success;
    try {
        success = !cancel.call();
    } catch (Exception e) {
        Exceptions.printStackTrace(e);
        success = false;
    }
    LOG.log(Level.FINER, "Canceled: {0}", !success);  //NOI18N
    return Pair.<Boolean,Collection<? extends URL>>of(success,result);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:38,代码来源:IndexBinaryWorkPool.java

示例5: testTake

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * A taken submitted task is completed
 */
public void testTake()
    throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    cs.submit(new StringTask());
    Future f = cs.take();
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:ExecutorCompletionServiceTest.java

示例6: testTake2

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * Take returns the same future object returned by submit
 */
public void testTake2() throws InterruptedException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    Future f1 = cs.submit(new StringTask());
    Future f2 = cs.take();
    assertSame(f1, f2);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:ExecutorCompletionServiceTest.java

示例7: configReloaded

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
private void configReloaded(List<DelayedConfigResponses.DelayedConfigResponse> responses, String logPre) {
    if (log.isLoggable(LogLevel.DEBUG)) {
        log.log(LogLevel.DEBUG, logPre + "Start of configReload: " + responses.size() + " requests on delayed requests queue");
    }
    int responsesSent = 0;
    CompletionService<Boolean> completionService = new ExecutorCompletionService<>(executorService);
    while (!responses.isEmpty()) {
        DelayedConfigResponses.DelayedConfigResponse delayedConfigResponse = responses.remove(0);
        // Discard the ones that we have already answered
        // Doing cancel here deals with the case where the timer is already running or has not run, so
        // there is no need for any extra check.
        if (delayedConfigResponse.cancel()) {
            if (log.isLoggable(LogLevel.DEBUG)) {
                logRequestDebug(LogLevel.DEBUG, logPre + "Timer cancelled for ", delayedConfigResponse.request);
            }
            // Do not wait for this request if we were unable to execute
            if (addToRequestQueue(delayedConfigResponse.request, false, completionService)) {
                responsesSent++;
            }
        } else {
            log.log(LogLevel.DEBUG, logPre + "Timer already cancelled or finished or never scheduled");
        }
    }

    for (int i = 0; i < responsesSent; i++) {
        try {
            completionService.take();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    log.log(LogLevel.DEBUG, logPre + "Finished reloading " + responsesSent + " requests");
}
 
开发者ID:vespa-engine,项目名称:vespa,代码行数:35,代码来源:RpcServer.java

示例8: exec

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * 
		*@name 将工作列表通过指定线程数来处理,全部执行后才返回,一次执行一个任务
		*@Description 相关说明 
		*@Time 创建时间:2014年12月27日上午10:02:02
 */
public static <T,V> void exec(final List<T> workList,int threadCount,final TaskProcess<T> process){
	if (workList == null || workList.isEmpty()) {
		return ;
	}
	
	ExecutorService pool = Executors.newFixedThreadPool(threadCount); 
	CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(pool);
	int workSize = workList.size();
	for (int i = 0; i < workSize; i++) {
		final int index = i;
		completionService.submit(new Callable<Integer>() {
			@Override
			public Integer call() throws Exception {
				process.exec(index,workList.get(index));
				return 1;
			}
		});
	}
	
	//等待全部执行完成
	for (int i = 0; i < workSize; i++) {
           try {
			completionService.take();
		} 
           catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	pool.shutdown();
}
 
开发者ID:yinshipeng,项目名称:sosoapi-base,代码行数:38,代码来源:TaskUtils.java

示例9: process

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
@Override
public void process(final Exchange exchange) throws Exception {
    final int count = getHeaderValue(exchange, HEADER_ITERATIONS);
    final int threads = getHeaderValue(exchange, HEADER_THREADS);
    PerformanceTestEndpoint endpoint = (PerformanceTestEndpoint)getEndpoint();
    if (endpoint != null) {
        final DefaultConsumer consumer = (DefaultConsumer)endpoint.getConsumer();
        ExecutorService executor = exchange.getContext().getExecutorServiceManager().newFixedThreadPool(this, "perf", threads);
        CompletionService<Exchange> tasks = new ExecutorCompletionService<Exchange>(executor);

        // StopWatch watch = new StopWatch();  // if we want to clock how long it takes
        for (int i = 0; i < count; i++) {
            tasks.submit(new Callable<Exchange>() {
                @Override
                public Exchange call() throws Exception {
                    Exchange exch = ExchangeHelper.createCopy(exchange, false);
                    try {
                        consumer.getProcessor().process(exch);
                    } catch (final Exception e) {
                        exch.setException(e);
                    }
                    return exch;
                }
            });
        }
        
        for (int i = 0; i < count; i++) {
            // Future<Exchange> result = tasks.take();
            tasks.take();       // wait for all exchanges to complete
        }
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:33,代码来源:PerformanceTestComponent.java

示例10: mix

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
@Override
public void mix() throws InterruptedException {
	MixTarget mixTarget = this.getMixTarget();
	MixStrategy mixStrategy = this.getMixStrategy();
	MixProcesser mixProcesser = this.getMixProcesser();
	mixTarget.initial();
	final ExecutorService executorService =
	//Executors.newCachedThreadPool();
	Executors.newFixedThreadPool(100);
	final CompletionService<MixBlock> competeService = new ExecutorCompletionService<MixBlock>(executorService);
	mixProcesser.start();
	MixBlocks blocks = mixTarget.blocks();
	for (MixBlock mixBlock : blocks) {
		competeService.submit(new Callable<MixBlock>() {
			@Override
			public MixBlock call() throws Exception {
				mixStrategy.action(mixBlock);
				mixBlock.persist();
				return mixBlock;
			}
		});
	}
	// waiting for all the piece task 
	for (int i = 0; i < blocks.size(); i++) {
		Future<MixBlock> future = competeService.take();
		MixBlock block = null;
		try {
			block = future.get();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		mixProcesser.processed(block);
	}
	mixTarget.free();
	mixProcesser.completed();
	executorService.shutdownNow();
}
 
开发者ID:daileyet,项目名称:ViMixer,代码行数:38,代码来源:AbstractMixer.java

示例11: executeCloseTask

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * @param completionService threadPool to execute the closing tasks
 * @param thrown store the exceptions
 * @param paths arrayList to store the paths written
 * @return if close tasks executed successful
 */
boolean executeCloseTask(CompletionService<Void> completionService,
    List<IOException> thrown, List<Path> paths)
    throws InterruptedException, ExecutionException {
  for (final Map.Entry<String, SinkWriter> writersEntry : writers.entrySet()) {
    if (LOG.isTraceEnabled()) {
      LOG.trace("Submitting close of " + ((WriterAndPath) writersEntry.getValue()).p);
    }
    completionService.submit(new Callable<Void>() {
      @Override public Void call() throws Exception {
        WriterAndPath wap = (WriterAndPath) writersEntry.getValue();
        Path dst = closeWriter(writersEntry.getKey(), wap, thrown);
        paths.add(dst);
        return null;
      }
    });
  }
  boolean progress_failed = false;
  for (int i = 0, n = this.writers.size(); i < n; i++) {
    Future<Void> future = completionService.take();
    future.get();
    if (!progress_failed && reporter != null && !reporter.progress()) {
      progress_failed = true;
    }
  }
  return progress_failed;
}
 
开发者ID:apache,项目名称:hbase,代码行数:33,代码来源:WALSplitter.java

示例12: fail_fast

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
@Test(timeout = 11000)
public void fail_fast() throws InterruptedException, ExecutionException {
  ExecutorService executorService = Executors.newFixedThreadPool(2);
  CompletionService<Long> completionService = new ExecutorCompletionService<Long>(executorService);

  BlockingQueue<OWLCompositeObject> queue = new LinkedBlockingQueue<OWLCompositeObject>();
  BlockingQueue<OntologySetup> ontologyQueue = new LinkedBlockingQueue<OntologySetup>();
  OwlOntologyProducer producer =
      new OwlOntologyProducer(queue, ontologyQueue, new AtomicInteger(), graph);
  OntologySetup ontologyConfig = new OntologySetup();

  ontologyConfig.setUrl("http://localhost:10000/foo.owl");

  List<Future<?>> futures = new ArrayList<>();
  futures.add(completionService.submit(producer));
  futures.add(completionService.submit(producer));
  Thread.sleep(1000);
  ontologyQueue.put(ontologyConfig);

  expectedException.expect(ExecutionException.class);
  while (futures.size() > 0) {
    Future<?> completedFuture = completionService.take();
    futures.remove(completedFuture);
    completedFuture.get();
  }

  executorService.shutdown();
  executorService.awaitTermination(10, TimeUnit.SECONDS);

}
 
开发者ID:SciGraph,项目名称:SciGraph,代码行数:31,代码来源:OwlOntologyProducerFailFastTest.java

示例13: doFsStress

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
/**
 * Start the client threads that will generate requests against the
 * {@link FileSystem}. This method waits until all the threads have
 * finished executing. Each thread is expected to exit when its
 * execution time has elapsed.
 *
 * @throws InterruptedException
 * @throws IOException
 */
private void doFsStress()
    throws InterruptedException, IOException {

  // Start the threads.
  final ExecutorService executor = Executors.newFixedThreadPool(
      (int) params.getNumThreads(),
      (new ThreadFactoryBuilder().setNameFormat(
          "FsStress-Client-Thread-%d").build()));

  final CompletionService<Object> ecs =
      new ExecutorCompletionService<>(executor);

  LOG.info("Starting {} client thread{}.",
      params.getNumThreads(),
      params.getNumThreads() > 1 ? "s" : "");
  
  for (long t = 0; t < params.getNumThreads(); ++t) {
    ecs.submit(new ClientThread(t));
  }

  // And wait for all threads to complete.
  for (long t = 0; t < params.getNumThreads(); ++t) {
    ecs.take();
  }
  executor.shutdown();
}
 
开发者ID:arp7,项目名称:FsPerfTest,代码行数:36,代码来源:FsStress.java

示例14: test

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
@org.junit.Test
public void test() {
    System.out.println("start");
    CompletionService<Boolean> compService = new ExecutorCompletionService<Boolean>(Executors.newCachedThreadPool());

    for (int i = 0; i < 10; i++) {
        compService.submit(new Callable<Boolean>() {

            @Override
            public Boolean call()
                throws Exception {
                Thread.sleep(10000);
                return true;
            }
        });
    }
    for (int i = 0; i < 10; i++) {
        try {
            Future<Boolean> take = compService.take();

            System.out.println(take.get(10, TimeUnit.MILLISECONDS));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
 
开发者ID:armangal,项目名称:SmartMonitoring,代码行数:28,代码来源:AbstractStateUpdaterThread.java

示例15: doUpload

import java.util.concurrent.CompletionService; //导入方法依赖的package包/类
private void doUpload() {

		listener.onStart(indexes != null ? indexes.size() : getPartCount());
		parts = new ArrayBlockingQueue<Part>(Config.maxRead);
		CompletionService<String> cs = new ExecutorCompletionService<String>(executor);

		cs.submit(readTask);

		for (int i = 0; i < Config.maxUpload; i++) {
			cs.submit(new UploadTask("upload." + i));
		}

		// Wait all done. total count = maxUpload + 1.
		for (int i = 0; i <= Config.maxUpload; i++) {
			Future<String> future = null;
			try {
				future = cs.take();
				checkFuture(future);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}

		// Notify sever all done.
		Future<String> result = executor.submit(notifyTask);
		checkFuture(result);
		listener.onSuccess();
	}
 
开发者ID:clxy,项目名称:BigFileUploadJava,代码行数:29,代码来源:UploadFileService.java


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