Java Code Examples for java.util.concurrent.LinkedBlockingDeque#size()

The following examples show how to use java.util.concurrent.LinkedBlockingDeque#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PaxosTests.java    From rapid with Apache License 2.0 6 votes vote down vote up
/**
 * Wait and then verify all consensus decisions
 *
 * @param expectedSize expected size of each cluster
 * @param maxTries number of tries to checkSubject if the cluster has stabilized.
 * @param intervalInMs the time duration between checks.
 * @param decisions the reported consensus decisions
 */
private void waitAndVerifyAgreement(final int expectedSize, final int maxTries, final int intervalInMs,
                                    final LinkedBlockingDeque<List<Endpoint>> decisions)
                                    throws InterruptedException {
    int tries = maxTries;
    while (--tries > 0) {
        if (decisions.size() != expectedSize) {
            Thread.sleep(intervalInMs);
        }
        else {
            break;
        }
    }

    assertEquals(expectedSize, decisions.size());
    if (expectedSize > 0) {
        final List<Endpoint> first = decisions.getFirst();
        assertAll(first, decisions);
    }
}
 
Example 2
Source File: RequestThrottleFilter.java    From publick-sling-blog with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(final ServletRequest request, final ServletResponse response,
        final FilterChain chain) throws IOException, ServletException {

    final HttpServletRequest httpServletRequest = (HttpServletRequest)request;
    final HttpSession session = httpServletRequest.getSession(true);
    final String path = httpServletRequest.getPathInfo().toLowerCase();
    final String method = httpServletRequest.getMethod();

    if ("POST".equals(method) && path.startsWith(PublickConstants.SERVLET_PATH_PUBLIC)) {
        synchronized (session.getId().intern()) {
            LinkedBlockingDeque<Long> times = (LinkedBlockingDeque<Long>)session.getAttribute(TIMES);

            if (times == null) {
                times = new LinkedBlockingDeque<Long>();
                session.setAttribute(TIMES, times);
            }

            final long currentTimeMillis = System.currentTimeMillis();

            times.push(Long.valueOf(currentTimeMillis));
            final long cutoff = currentTimeMillis - (SECONDS * 1000);
            Long oldest = times.peekLast();
            while (oldest != null && oldest.longValue() < cutoff) {
                times.removeLast();
                oldest =  times.peekLast();
            }

            if (times.size() > HITS) {
                addResponseHeaderNoCache((HttpServletResponse)response);
                response.getWriter().println("Your request hit rate is too high;"
                        + " please press back, wait a few seconds, and then try again.");
                return;
            }
        }
    }
    chain.doFilter(request, response);
}
 
Example 3
Source File: RegressionTree.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public List<Integer> getFeatureIndices(){
    List<Integer> featureIndices = new ArrayList<Integer>();
    LinkedBlockingDeque<Node> queue = new LinkedBlockingDeque<Node>();
    queue.offer(this.root);
    while(queue.size()!=0){
        Node node = queue.poll();
        if (! node.isLeaf()){
            //don't add the feature for leaf node, as it is useless
            featureIndices.add(node.getFeatureIndex());
            queue.offer(node.getLeftChild());
            queue.offer(node.getRightChild());
        }
    }
    return featureIndices;
}
 
Example 4
Source File: MsgProcessingRepository.java    From ClusterDeviceControlPlatform with MIT License 4 votes vote down vote up
/**
 * 「数据统计」创建单设备组服务器缓存信息bean
 *
 * @param groupId    设备组 ID
 * @param needDetail 返回值是否附带所有统计细节
 * @return 单设备组服务器缓存信息bean
 */
private DeviceGroupItem createSingleDeviceGroup(int groupId, boolean needDetail) {
    List<DeviceStatusItem> deviceStatusItems = new ArrayList<>(DeviceSetting.MAX_DEVICE_ID);
    int deviceCount = DeviceSetting.MAX_DEVICE_ID;
    int usingCount = 0;
    int chargingCount = 0;
    int fullCount = 0;
    int uninitCount = 0;
    int msgCount = -1;
    LinkedBlockingDeque<MsgReplyDeviceStatus> deque = touchMessageQueue(groupId);
    if (deque != null) {
        msgCount = deque.size();
    }

    for (int deviceId = 1; deviceId <= DeviceSetting.MAX_DEVICE_ID; deviceId++) {
        StatusItem chargeStatus = dbPresenter.obtainStatusByCache(groupId, deviceId, MsgReplyDeviceStatus.Type.CHARGE);
        int charge = chargeStatus.getStatus();
        switch (charge) {
            case ChargeStatus.UNINIT:
                uninitCount++;
                break;
            case ChargeStatus.USING:
                usingCount++;
                break;
            case ChargeStatus.CHARGING:
                chargingCount++;
                break;
            case ChargeStatus.FULL:
                fullCount++;
                break;
            default:
                break;
        }
        if (charge < 0) {
            charge = 0;
        }
        StatusItem workStatus = dbPresenter.obtainStatusByCache(groupId, deviceId, MsgReplyDeviceStatus.Type.WORK);

        int work = workStatus.getStatus();
        if (work < 4) {
            work = 1;
        }
        DeviceStatusItem deviceStatusItem = new DeviceStatusItem(deviceId, charge, work);
        deviceStatusItems.add(deviceStatusItem);
    }
    if (!needDetail) {
        deviceStatusItems = null;
    }
    return new DeviceGroupItem(groupId, deviceStatusItems, msgCount, deviceCount, usingCount, chargingCount, fullCount, uninitCount);
}
 
Example 5
Source File: LBDSpliterator.java    From streamsupport with GNU General Public License v2.0 4 votes vote down vote up
private LBDSpliterator(LinkedBlockingDeque<E> queue) {
    this.queue = queue;
    this.est = queue.size();
    this.queueLock = getQueueLock(queue);
}