Description
Polls the blocking queue, waiting up to the POLLING_TIMEOUT (in seconds) if necessary for an element to become available.
License
Open Source License
Parameter
Parameter | Description |
---|
T | a parameter |
queue | a parameter |
clazz | a parameter |
Exception
Parameter | Description |
---|
RuntimeException | if interrupted while waiting. |
Return
The removed object from the blocking queue, null in case of timeout
Declaration
public static <T> T waitForResponseObject(BlockingQueue<Object> queue, Class<T> clazz)
Method Source Code
//package com.java2s;
/*/*from w w w. ja va 2 s . c o m*/
* Copyright (C) 2008 Universidade Federal de Campina Grande
*
* This file is part of Commune.
*
* Commune is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class Main {
public static final long POLLING_TIMEOUT = 30L;
/**
* Polls the blocking queue, waiting up to the POLLING_TIMEOUT (in seconds) if necessary
* for an element to become available.
* @param <T>
* @param queue
* @param clazz
* @throws RuntimeException if interrupted while waiting.
* @return The removed object from the blocking queue, null in case of timeout
*/
public static <T> T waitForResponseObject(BlockingQueue<Object> queue, Class<T> clazz) {
return waitForResponseObject(queue, clazz, POLLING_TIMEOUT);
}
/**
* Polls the blocking queue, waiting up to the timeout if necessary
* for an element to become available.
* @param <T>
* @param queue
* @param clazz
* @param timeout
* @throws RuntimeException if interrupted while waiting.
* @return The removed object from the blocking queue, null in case of timeout
*/
@SuppressWarnings("unchecked")
public static <T> T waitForResponseObject(BlockingQueue<Object> queue, Class<T> clazz, long timeout) {
T takingResponse = null;
try {
takingResponse = (T) queue.poll(timeout, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return takingResponse;
}
}
Related
- waitFor(long period)
- waitFor(Object obj)
- waitFor(Semaphore semaphore, long timeoutInMillis)
- waitForMinutes(long time)
- waitForProcess(Process process, String name)
- waitForSignal(CountDownLatch b)
- weekendMinutesBetween(Date date1, Date date2)
- zeroOutMs(long timestamp)