本文整理汇总了Java中java.util.concurrent.BlockingQueue.clear方法的典型用法代码示例。如果您正苦于以下问题:Java BlockingQueue.clear方法的具体用法?Java BlockingQueue.clear怎么用?Java BlockingQueue.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.BlockingQueue
的用法示例。
在下文中一共展示了BlockingQueue.clear方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connectWithTimeout
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* JMX连接
* @param url
* JMX连接地址
* @param jmxUser
* JMX授权用户 null为无授权用户
* @param jmxPassword
* JMX授权密码 null为无授权密码
* @param timeout
* 超时时间
* @param unit
* 超时单位
* @return
* @throws IOException
*/
public static JMXConnector connectWithTimeout( final JMXServiceURL url,String jmxUser,String jmxPassword, long timeout, TimeUnit unit) throws Exception {
final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1);
ExecuteThreadUtil.execute(() -> {
try {
JMXConnector connector;
if(jmxUser != null && jmxPassword != null){
Map<String,Object> env = new HashMap<>();
String[] credentials = new String[] { jmxUser, jmxPassword };
env.put(JMXConnector.CREDENTIALS, credentials);
connector = JMXConnectorFactory.connect(url,env);
}else{
connector = JMXConnectorFactory.connect(url,null);
}
if (!blockingQueue.offer(connector))
connector.close();
} catch (Throwable t) {
blockingQueue.offer(t);
}
});
Object result = BlockingQueueUtil.getResult(blockingQueue,timeout,unit);
blockingQueue.clear();
if (result instanceof JMXConnector){
return (JMXConnector) result;
}else if (result == null){
throw new SocketTimeoutException("Connect timed out: " + url);
}else if(result instanceof Throwable){
throw new IOException("JMX Connect Failed : " + url,((Throwable) result));
}
return null;
}
示例2: testListeningDecorator_cancelled
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
public void testListeningDecorator_cancelled() throws Exception {
ScheduledThreadPoolExecutor delegate = new ScheduledThreadPoolExecutor(1);
BlockingQueue<?> delegateQueue = delegate.getQueue();
ListeningScheduledExecutorService service = listeningDecorator(delegate);
ListenableFuture<?> future;
ScheduledFuture<?> delegateFuture;
Runnable runnable = new Runnable() {
@Override public void run() {}
};
future = service.schedule(runnable, 5, TimeUnit.MINUTES);
future.cancel(true);
assertTrue(future.isCancelled());
delegateFuture = (ScheduledFuture<?>) delegateQueue.element();
assertTrue(delegateFuture.isCancelled());
delegateQueue.clear();
future = service.scheduleAtFixedRate(runnable, 5, 5, TimeUnit.MINUTES);
future.cancel(true);
assertTrue(future.isCancelled());
delegateFuture = (ScheduledFuture<?>) delegateQueue.element();
assertTrue(delegateFuture.isCancelled());
delegateQueue.clear();
future = service.scheduleWithFixedDelay(runnable, 5, 5, TimeUnit.MINUTES);
future.cancel(true);
assertTrue(future.isCancelled());
delegateFuture = (ScheduledFuture<?>) delegateQueue.element();
assertTrue(delegateFuture.isCancelled());
}
示例3: clearAllTempQueueForShadowPR
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
protected void clearAllTempQueueForShadowPR(final int bucketId) {
List<PartitionedRegion> colocatedWithList =
ColocationHelper.getColocatedChildRegions(partitionedRegion);
for (PartitionedRegion childRegion : colocatedWithList) {
if (childRegion.isShadowPR()) {
AbstractGatewaySender sender = childRegion.getParallelGatewaySender();
if (sender == null) {
return;
}
AbstractGatewaySenderEventProcessor eventProcessor = sender.getEventProcessor();
if (eventProcessor == null) {
return;
}
ConcurrentParallelGatewaySenderQueue queue =
(ConcurrentParallelGatewaySenderQueue) eventProcessor.getQueue();
if (queue == null) {
return;
}
BlockingQueue<GatewaySenderEventImpl> tempQueue = queue.getBucketTmpQueue(bucketId);
if (tempQueue != null) {
synchronized (tempQueue) {
for (GatewaySenderEventImpl event : tempQueue) {
event.release();
}
tempQueue.clear();
}
}
}
}
}
示例4: getContainerInfo
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* 获取容器信息
* @param containerId
* @return
*/
public static ContainerInfo getContainerInfo(String containerId){
String cacheKey = "containerInfoCacheKey" + containerId;
final ContainerInfo[] containerInfo = {(ContainerInfo) getCache(cacheKey)};
if (containerInfo[0] != null){
return containerInfo[0];
}else {
synchronized (containerId.intern()) {
try {
int timeOut = 45;
final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1);
//阻塞队列异步执行
ExecuteThreadUtil.execute(() -> {
try {
containerInfo[0] = docker.inspectContainer(containerId);
setCache(cacheKey, containerInfo[0]);
blockingQueue.offer(containerInfo[0]);
} catch (Throwable t) {
blockingQueue.offer(t);
}
});
//超时45秒
Object result = BlockingQueueUtil.getResult(blockingQueue, timeOut, TimeUnit.SECONDS);
blockingQueue.clear();
if (result instanceof ContainerInfo) {
return (ContainerInfo) result;
}else if (result == null) {
log.error("docker 容器Info获取{}秒超时:{}",timeOut,containerId);
return null;
}else if (result instanceof Throwable) {
log.error("docker 容器Info获取异常",result);
return null;
}else {
log.error("未知结果类型:{}",result);
return null;
}
} catch (Exception e) {
log.error("",e);
return null;
}
}
}
}
示例5: getContainers
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* 获取容器列表
* @param containersParam
* @return
*/
public static List<Container> getContainers(DockerClient.ListContainersParam containersParam) {
String cacheKey = "getContainer" + containersParam.value();
final List<Container> containers = (List<Container>) CacheByTimeUtil.getCache(cacheKey);
if (containers != null) {
return containers;
}
try {
int timeOut = 45;
final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1);
//阻塞队列异步执行
ExecuteThreadUtil.execute(() -> {
try {
List<Container> containerList = docker.listContainers(containersParam);
setCache(cacheKey, containerList);
blockingQueue.offer(containerList);
} catch (Throwable t) {
blockingQueue.offer(t);
}
});
//超时
Object result = BlockingQueueUtil.getResult(blockingQueue, timeOut, TimeUnit.SECONDS);
blockingQueue.clear();
if (result instanceof List) {
return (List<Container>) result;
}else if (result == null) {
log.error("docker 容器 List 获取{}秒超",timeOut);
return new ArrayList<>();
}else if (result instanceof Throwable) {
log.error("docker 容器 List 获取异常",result);
return new ArrayList<>();
}else {
log.error("未知结果类型:{}",result);
return new ArrayList<>();
}
} catch (Exception e) {
log.error("",e);
return new ArrayList<>();
}
}
示例6: validateInternetStatus
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Check if internet is available if we can open a http connection to google
*
* @param numberRetries Number of retries of this request.
*/
public static void validateInternetStatus(final int numberRetries) {
//Avoid having multiple internet updates running, let just the last run
BlockingQueue<Runnable> tasksQueue = threadPool.getQueue();
if (!tasksQueue.isEmpty()) {
tasksQueue.clear();
}
threadPool.execute(new Runnable() {
@Override
public void run() {
int retries = numberRetries;
do {
try {
URL url = new URL(mInternetAddress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(mInternetValidationTimeout);
isInternetAvailable =
conn.getResponseCode() == HTTP_200
|| conn.getResponseCode() == HTTP_201;
LOG.v(LOG_TAG, "Response code -> " + conn.getResponseCode());
isInternetAvailable &= conn
.getURL().toString()
.startsWith(mInternetHttpValidationAddress) || conn
.getURL().toString()
.startsWith(mInternetHttpsValidationAddress);
LOG.v(LOG_TAG, "URL -> ", conn.getURL().toString());
conn.disconnect();
if (isInternetAvailable) {
LOG.v(LOG_TAG, "INTERNET IS AVAILABLE");
retries = 0;
} else {
LOG.v(LOG_TAG, "INTERNET IS NOT AVAILABLE");
retries--;
Thread.sleep(ONE_SECOND);
}
} catch (Exception ex) {
LOG.e(LOG_TAG, ex, "ConnectivityReceiver isConnected");
isInternetAvailable = false;
}
} while (retries > 0);
notifyConnectionStatus();
}
});
}