Example usage for java.util.concurrent ConcurrentMap size

List of usage examples for java.util.concurrent ConcurrentMap size

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentMap size.

Prototype

int size();

Source Link

Document

Returns the number of key-value mappings in this map.

Usage

From source file:org.silverpeas.core.viewer.service.ViewServiceConcurrencyDemonstrationTest.java

@Test
public void demonstrateConcurrencyManagementWithTwoDifferentConversionsAtSameTime() throws Exception {
    if (canPerformViewConversionTest()) {
        final List<Throwable> throwables = new ArrayList<>();

        final ConcurrentMap serviceCache = (ConcurrentMap) FieldUtils.readField(viewService, "cache", true);
        assertThat(serviceCache.size(), is(0));

        final int NB_VIEW_CALLS = 100;

        for (int i = 0; i < NB_VIEW_CALLS; i++) {
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override//from  w w  w.j  a  v a2  s . c  om
                public void run() {
                    try {
                        viewService.getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.odp")));
                    } catch (Exception ignore) {
                    }
                }
            }));
            SubThreadManager.addAndStart(new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        viewService.getDocumentView(ViewerContext.from(getSimpleDocumentNamed("file.pdf")));
                    } catch (Exception ignore) {
                    }
                }
            }));
        }

        SubThreadManager.addAndStart(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);

                    // Technical verification
                    assertThat(getTemporaryPath().listFiles(), arrayWithSize(4));

                } catch (Throwable e) {
                    throwables.add(e);
                    SubThreadManager.killAll();
                }
            }
        }));

        // Waiting for all thread ends
        SubThreadManager.joinAll(60000);

        for (Throwable throwable : throwables) {
            throw new RuntimeException(throwable);
        }

        assertThat(serviceCache.size(), is(0));
        assertThat(getTemporaryPath().listFiles(), arrayWithSize(4));
    }
}

From source file:org.wso2.carbon.stream.processor.core.persistence.PersistenceManager.java

private void persistAndSendControlMessage() {
    ConcurrentMap<String, SiddhiAppRuntime> siddhiAppRuntimeMap = StreamProcessorDataHolder.getSiddhiManager()
            .getSiddhiAppRuntimeMap();//from   w w  w . j  ava2  s  .c o m
    String[] siddhiRevisionArray = new String[siddhiAppRuntimeMap.size()];
    int siddhiAppCount = 0;
    for (SiddhiAppRuntime siddhiAppRuntime : siddhiAppRuntimeMap.values()) {
        PersistenceReference persistenceReference = siddhiAppRuntime.persist();
        Future fullStateFuture = persistenceReference.getFullStateFuture();
        try {
            if (fullStateFuture != null) {
                fullStateFuture.get(60000, TimeUnit.MILLISECONDS);
            } else {
                for (Future future : persistenceReference.getIncrementalStateFuture()) {
                    future.get(60000, TimeUnit.MILLISECONDS);
                }
            }
        } catch (Throwable e) {
            log.error(
                    "Active Node: Persisting of Siddhi app is not successful. Check if app deployed properly");
        }
        siddhiRevisionArray[siddhiAppCount] = sequenceIDGenerator.incrementAndGet()
                + HAConstants.PERSISTED_APP_SPLIT_DELIMITER + persistenceReference.getRevision();
        siddhiAppCount++;
        if (log.isDebugEnabled()) {
            log.debug("Revision " + persistenceReference.getRevision() + " of siddhi App "
                    + siddhiAppRuntime.getName() + " persisted successfully");
        }
    }
    if (haManager != null && haManager.isActiveNode() && haManager.isPassiveNodeAdded()) {
        if (log.isDebugEnabled()) {
            log.debug("Control Message is sent to the passive node - " + Arrays.toString(siddhiRevisionArray));
        }
        sendControlMessageToPassiveNode(siddhiRevisionArray);
    }
    if (StreamProcessorDataHolder.getNodeInfo() != null) {
        StreamProcessorDataHolder.getNodeInfo().setLastPersistedTimestamp(System.currentTimeMillis());
    }
    log.info("siddhi Apps are persisted successfully");
}