當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。