Java Thread Executor isCorrectCollectionSizeTimeOut(Collection collection, int size, long timeout, TimeUnit unit)

Here you can find the source of isCorrectCollectionSizeTimeOut(Collection collection, int size, long timeout, TimeUnit unit)

Description

Check size of a collection populated in another thread.

License

Open Source License

Parameter

Parameter Description
collection collection to check
size size limit
timeout the maximum time to wait
unit the time unit of the timeout argument

Exception

Parameter Description
InterruptedException if interrupted while waiting

Return

true if collection size is reached before timeout false if the timeout elapsed before collection size reached

Declaration

public static boolean isCorrectCollectionSizeTimeOut(Collection<?> collection, int size, long timeout,
        TimeUnit unit) throws InterruptedException 

Method Source Code

//package com.java2s;
/*//  www  .j a  v  a  2  s .  c  om
 * IronJacamar, a Java EE Connector Architecture implementation
 * Copyright 2015, Red Hat Inc, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the Eclipse Public License 1.0 as
 * published by the Free Software Foundation.
 *
 * This software 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 Eclipse
 * Public License for more details.
 *
 * You should have received a copy of the Eclipse 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.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Check size of a collection populated in another thread. Collection is supposed to be thread safe
     * @param collection collection to check
     * @param size size limit
     * @param timeout the maximum time to wait
     * @param unit the time unit of the timeout argument
        
     * @return {@code true} if collection size is reached before timeout
     *         {@code false} if the timeout elapsed before collection size reached
     * @throws InterruptedException if interrupted while waiting
     */
    public static boolean isCorrectCollectionSizeTimeOut(Collection<?> collection, int size, long timeout,
            TimeUnit unit) throws InterruptedException {
        Runnable task = () -> {
            try {
                while (collection.size() != size) {

                    TimeUnit.MILLISECONDS.sleep(10);
                }
            } catch (InterruptedException e) {
                throw new IllegalStateException("task interrupted", e);
            }
        };
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(task);
        executor.shutdown();
        return executor.awaitTermination(timeout, unit);
    }
}

Related

  1. convert(Runnable runnable, T result)
  2. getActiveHelperInstanceCount(String helperName)
  3. getChannelService()
  4. getConstantLookup()
  5. getThreadExecutor(int nThreads)
  6. isRegularlyDone(Future future)
  7. isShutdown(Executor executor)
  8. isShutDown(ExecutorService executorService)
  9. setTimeout(Long duration, Runnable csr)