Java TimeUnit Usage mergeResults(List>> futures)

Here you can find the source of mergeResults(List>> futures)

Description

Since keys can appear more than one time e.g.

License

Open Source License

Parameter

Parameter Description
futures the list of futures whose results should be merged
K the type of key

Exception

Parameter Description
InterruptedException if the process is interrupted
ExecutionException if there is an error while getting all keys

Return

the set of keys

Declaration

private static <K> Set<K> mergeResults(List<Future<Set<K>>> futures)
        throws InterruptedException, ExecutionException 

Method Source Code

//package com.java2s;
/*// w ww  . j  a v  a 2  s  . c  o  m
 * ModeShape (http://www.modeshape.org)
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * See the AUTHORS.txt file in the distribution for a full listing of 
 * individual contributors.
 *
 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
 * is licensed to you under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * ModeShape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Main {
    /**
     * Since keys can appear more than one time e.g. multiple owners in a distributed cache,
     * they all must be merged into a Set.
     * @param futures the list of futures whose results should be merged
     * @param <K> the type of key
     * @return the set of keys
     * @throws InterruptedException if the process is interrupted
     * @throws ExecutionException if there is an error while getting all keys
     */
    private static <K> Set<K> mergeResults(List<Future<Set<K>>> futures)
            throws InterruptedException, ExecutionException {
        // todo use ConcurrentHashSet and merge as the results appear
        Set<K> allKeys = new HashSet<K>();
        while (true) {
            // Get the next future that is ready ...
            Iterator<Future<Set<K>>> futureIter = futures.iterator();
            while (futureIter.hasNext()) {
                Future<Set<K>> future = futureIter.next();
                try {
                    // But done't wait too long for this future ...
                    Set<K> keys = future.get(100, TimeUnit.MILLISECONDS);
                    // We got some keys, so this future is done and should be removed from our list ...
                    futureIter.remove();
                    allKeys.addAll(keys);
                } catch (TimeoutException e) {
                    // continue;
                }
            }
            if (futures.isEmpty())
                break;
        }
        return allKeys;
    }
}

Related

  1. isTimedOut(long start, long timeout)
  2. isValidTTL(final Integer ttlDurationInSeconds)
  3. julianDayToMillis(int julianDay)
  4. logDuration(org.slf4j.Logger log, String prefix, long startTimeNanos)
  5. logWatchStop(long start, String... params)
  6. millisBetweenNanoTimes(long startNanos, long endNanos)
  7. millisecondsToHumanReadable(long duration)
  8. millisElapsedSince(long startNanoTime)
  9. millisToDays(long millisLocal)