get Result from BlockingQueue - Java java.util.concurrent

Java examples for java.util.concurrent:BlockingQueue

Description

get Result from BlockingQueue

Demo Code

/*//  w w  w . j a  v a  2s  . c  o  m
 * www.yiji.com Inc.
 * Copyright (c) 2016 All Rights Reserved
 */
import java.io.InterruptedIOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class Main{
    
    public static Object getResult(BlockingQueue<Object> blockingQueue,
            long timeout, TimeUnit unit) throws InterruptedIOException {
        Object result;
        try {
            result = blockingQueue.poll(timeout, unit);
            if (result == null) {
                if (!blockingQueue.offer(""))
                    result = blockingQueue.take();
            }
        } catch (InterruptedException e) {
            throw ExceptionUtil.initCause(
                    new InterruptedIOException(e.getMessage()), e);
        }
        return result;
    }
}

Related Tutorials