當前位置: 首頁>>代碼示例>>Java>>正文


Java BlockingQueue.clear方法代碼示例

本文整理匯總了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;
}
 
開發者ID:DevopsJK,項目名稱:SuitAgent,代碼行數:49,代碼來源:JMXConnectWithTimeout.java

示例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());
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:34,代碼來源:MoreExecutorsTest.java

示例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();
        }
      }
    }
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:33,代碼來源:PartitionedRegionDataStore.java

示例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;
            }
        }
    }
}
 
開發者ID:DevopsJK,項目名稱:SuitAgent,代碼行數:50,代碼來源:DockerUtil.java

示例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<>();
    }

}
 
開發者ID:DevopsJK,項目名稱:SuitAgent,代碼行數:49,代碼來源:DockerUtil.java

示例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();
        }

    });
}
 
開發者ID:Mindera,項目名稱:skeletoid,代碼行數:59,代碼來源:ConnectivityReceiver.java


注:本文中的java.util.concurrent.BlockingQueue.clear方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。