本文整理汇总了Java中java.util.concurrent.ExecutorCompletionService.poll方法的典型用法代码示例。如果您正苦于以下问题:Java ExecutorCompletionService.poll方法的具体用法?Java ExecutorCompletionService.poll怎么用?Java ExecutorCompletionService.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ExecutorCompletionService
的用法示例。
在下文中一共展示了ExecutorCompletionService.poll方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: waitForCompletion
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private void waitForCompletion(ExecutorCompletionService<Boolean> completer, int numtasks) throws ExecutionException {
int remainingTasks = numtasks;
while (remainingTasks > 0) {
try {
Future<Boolean> completed = completer.poll(4, TimeUnit.SECONDS);
if (completed == null) {
System.err.println("Completer timed out");
throw new RuntimeException(new TimeoutException());
}
else {
completed.get();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
remainingTasks--;
}
}
示例2: handleN
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private Response handleN(long start, long deadline,
ExecutorCompletionService<Response> completionService, List<Destination> destinations,
ResultMerger merger, BaseMessage message, Consistency c) {
List<Response> responses = new ArrayList<>();
int wantedResults = (Integer) c.getParameters().get("n");
int sucessfulSoFar = 0;
while (start <= deadline) {
Response r = null;
try {
Future<Response> future = completionService.poll(deadline - start, TimeUnit.MILLISECONDS);
r = future.get();
if (r != null){
responses.add(r);
sucessfulSoFar++;
}
if (sucessfulSoFar >= wantedResults) {
break;
}
} catch (InterruptedException | ExecutionException e) {
continue;
}
start = System.currentTimeMillis();
}
return merger.merge(responses, message);
}
示例3: execute
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private void execute(String[] args) throws IOException {
if (args.length != 3) {
usage();
System.exit(1);
}
if (args[0].contains("-h")) {
usage();
System.exit(0);
}
int numThreads = Integer.parseInt(args[0]);
ArrayBlockingQueue<Path> queue = new ArrayBlockingQueue<>(1000);
rootDir = Paths.get(args[1]);
QueueFiller filler = new QueueFiller(rootDir, queue, numThreads);
new Thread(filler).start();
outputDir = Paths.get(args[2]);
Files.createDirectories(outputDir);
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
ExecutorCompletionService<Integer> executorCompletionService = new ExecutorCompletionService<Integer>(executorService);
for (int i = 0; i < numThreads; i++) {
executorCompletionService.submit(new FileRunner(queue,
Files.newBufferedWriter(outputDir.resolve("mimes-"+i+".txt"))));
}
int completed = 0;
while (completed < numThreads) {
try {
Future<Integer> future = executorCompletionService.poll(1, TimeUnit.SECONDS);
if (future != null) {
completed++;
}
} catch (InterruptedException e) {
}
}
executorService.shutdown();
executorService.shutdownNow();
System.exit(0);
}
示例4: handleAll
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private Response handleAll(long start, long deadline,
ExecutorCompletionService<Response> completionService, List<Destination> destinations,
ResultMerger merger, BaseMessage message) {
List<Response> responses = new ArrayList<>();
while (start <= deadline) {
Response r = null;
try {
Future<Response> future = completionService.poll(deadline - start, TimeUnit.MILLISECONDS);
r = future.get();
if (r != null) {
responses.add(r);
}
} catch (InterruptedException | ExecutionException e) {
return new Response().withProperty("exception", "coordinator timeout");
}
if (r == null) {
return new Response().withProperty("exception", "coordinator timeout");
}
if (r.containsKey("exception")) {
return r;
}
if (responses.size() == destinations.size()) {
break;
}
start = System.currentTimeMillis();
}
if (responses.size() == destinations.size()) {
return merger.merge(responses, message);
} else {
return new Response().withProperty("exception", "coordinator timeout");
}
}
示例5: execute
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private void execute(String[] args) throws IOException {
if (args.length != 3) {
usage();
System.exit(1);
}
if (args[0].contains("-h")) {
usage();
System.exit(0);
}
int numThreads = Integer.parseInt(args[0]);
BufferedReader r = Files.newBufferedReader(Paths.get(args[1]));
ArrayBlockingQueue<DigestURLPair> queue = new ArrayBlockingQueue<DigestURLPair>(1000);
QueueFiller filler = new QueueFiller(r, queue, numThreads);
new Thread(filler).start();
rootDir = Paths.get(args[2]);
System.out.println("creating thread pool");
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
ExecutorCompletionService<Integer> executorCompletionService = new ExecutorCompletionService<Integer>(executorService);
System.out.println("about to start");
for (int i = 0; i < numThreads; i++) {
System.out.println("submitted "+i);
executorCompletionService.submit(new WGetter(queue));
}
int completed = 0;
while (completed < numThreads) {
try {
Future<Integer> future = executorCompletionService.poll(1, TimeUnit.SECONDS);
if (future != null) {
completed++;
}
} catch (InterruptedException e) {
}
}
executorService.shutdown();
executorService.shutdownNow();
System.exit(0);
}
示例6: execute
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
public void execute(String[] args) throws Exception {
if (args[0].equals("-h") || args[0].equals("--help")) {
usage();
System.exit(1);
}
int numThreads = Integer.parseInt(args[0]);
Path indexDir = Paths.get(args[1]);
String pClass = args[2];
//load index files into memory...there should only be 300 for now
File[] gzs = indexDir.toFile().listFiles();
numThreads = (gzs.length < numThreads) ? gzs.length : numThreads;
ArrayBlockingQueue<Path> paths = new ArrayBlockingQueue<>(gzs.length+numThreads);
Arrays.sort(gzs);
for (File f : gzs) {
paths.add(f.toPath());
}
for (int i = 0; i < numThreads; i++) {
paths.add(CCIndexReaderWrapper.POISON);
}
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
ExecutorCompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(executorService);
String[] newArgs = Arrays.copyOfRange(args, 3, args.length);
List<IndexRecordProcessor> procs = new ArrayList<>();
for (int i = 0; i < numThreads; i++) {
IndexRecordProcessor p = (IndexRecordProcessor) Class.forName(PACKAGE_NAME+"."+pClass).newInstance();
p.init(newArgs);
procs.add(p);
completionService.submit(new CCIndexReaderWrapper(paths, p));
}
int completed = 0;
while (completed < numThreads) {
Future<Integer> result = completionService.poll(1, TimeUnit.SECONDS);
if (result != null) {
completed++;
} else {
System.out.println("In completion loop: "+completed);
}
}
executorService.shutdown();
executorService.shutdownNow();
System.exit(1);
}
示例7: getAggregateValues
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
/**
* Get all aggregate values for all aggregators. This is the multi-threaded
* version.
* @param list The sorted list copy of values to aggregate.
* @param comparator A <code>Comparator</code> over T objects.
* @param parallelism The degree of parallelism.
* @return A <code>List</code> of <code>AggregateValues</code>.
*/
private <T> List<AggregateValue<T>> getAggregateValues(List<T> list,
Comparator<? super T> comparator, int parallelism)
{
List<PositionedAggregatorList<T>> listOfPals = new ArrayList<PositionedAggregatorList<T>>(parallelism);
// Initialize it with null elements, so that when "set" is called later,
// Java won't complain about the size being zero.
for (int p = 0; p < parallelism; p++)
listOfPals.add(null);
// Lazy-initialize the ExecutorCompletionService.
ExecutorCompletionService<PositionedAggregatorList<T>> service = initializeService();
int size = list.size();
// Submit AggregateRunners that are specific to this task.
for (int p = 0; p < parallelism; p++)
{
int startIndex = (size * p) / parallelism;
int endIndex = (size * (p + 1)) / parallelism - 1;
service.submit(new AggregateRunner<T>(myAggregators, list, p, comparator, startIndex, endIndex, amIUsingSuperAggregation, myProperties));
}
// Wait until all Threads have created their PositionedAggregatorList.
// If an Exception is thrown, it will be caught in the form of an
// ExecutionException, and wrapped in an UnsupportedOperationException.
int numPALs = 0;
while (numPALs < parallelism)
{
try
{
Future<PositionedAggregatorList<T>> future = service.poll(1, TimeUnit.SECONDS);
if (future != null)
{
// Something completed and is available. Add it to the list of
// PALs in its proper position.
PositionedAggregatorList<T> pal = future.get();
listOfPals.set(pal.getPosition(), pal);
numPALs++;
}
}
catch(InterruptedException ignored) {}
catch(ExecutionException e)
{
throw new UnsupportedOperationException(e.getClass().getName() +
" caught while aggregating.", e);
}
}
return Aggregations.mergeLists(listOfPals, comparator, amIUsingSuperAggregation, myProperties);
}