当前位置: 首页>>代码示例>>Java>>正文


Java ConcurrentMap.clear方法代码示例

本文整理汇总了Java中java.util.concurrent.ConcurrentMap.clear方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentMap.clear方法的具体用法?Java ConcurrentMap.clear怎么用?Java ConcurrentMap.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ConcurrentMap的用法示例。


在下文中一共展示了ConcurrentMap.clear方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testConcurrentMap_computeIfAbsent_racy

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
 * Simulates races by modifying the map within the mapping function.
 */
@Test
public void testConcurrentMap_computeIfAbsent_racy() {
    final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
    final Long two = 2L;
    Function<Long,Long> f, g;

    // race not detected if function returns null
    f = (k) -> { map.put(two, 42L); return null; };
    assertNull(map.computeIfAbsent(two, f));
    assertEquals(42L, (long)map.get(two));

    map.clear();
    f = (k) -> { map.put(two, 42L); return 86L; };
    assertEquals(42L, (long)map.computeIfAbsent(two, f));
    assertEquals(42L, (long)map.get(two));

    // mapping function ignored if value already exists
    map.put(two, 99L);
    assertEquals(99L, (long)map.computeIfAbsent(two, f));
    assertEquals(99L, (long)map.get(two));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:Defaults.java

示例2: doTestConcurrentCreateGetsUniqueConnection

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
private void doTestConcurrentCreateGetsUniqueConnection(boolean createOnStart) throws Exception {
    final int numConnections = 2;

    final MockJMSConnectionFactory mock = new MockJMSConnectionFactory();
    cf = new JmsPoolConnectionFactory();
    cf.setConnectionFactory(mock);
    cf.setMaxConnections(numConnections);
    cf.setCreateConnectionOnStartup(createOnStart);
    cf.start();

    final ConcurrentMap<UUID, Connection> connections = new ConcurrentHashMap<>();
    final ExecutorService executor = Executors.newFixedThreadPool(numConnections);

    for (int i = 0; i < numConnections; ++i) {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    JmsPoolConnection pooled = (JmsPoolConnection) cf.createConnection();
                    MockJMSConnection wrapped = (MockJMSConnection) pooled.getConnection();
                    connections.put(wrapped.getConnectionId(), pooled);
                } catch (JMSException e) {
                }
            }
        });
    }

    executor.shutdown();
    assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS));

    assertEquals("Should have all unique connections", numConnections, connections.size());

    connections.clear();
    cf.stop();
}
 
开发者ID:messaginghub,项目名称:pooled-jms,代码行数:37,代码来源:JmsPoolConnectionFactoryTest.java

示例3: testConcurrentMap_computeIfPresent_racy

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
 * Simulates races by modifying the map within the remapping function.
 */
@Test
public void testConcurrentMap_computeIfPresent_racy() {
    final AtomicBoolean b = new AtomicBoolean(true);
    final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
    final Long two = 2L;
    BiFunction<Long,Long,Long> f, g;

    for (Long val : new Long[] { null, 86L }) {
        map.clear();

        // Function not invoked if no mapping exists
        f = (k, v) -> { map.put(two, 42L); return val; };
        assertNull(map.computeIfPresent(two, f));
        assertNull(map.get(two));

        map.put(two, 42L);
        f = (k, v) -> { map.put(two, 86L); return val; };
        g = (k, v) -> {
            assertSame(two, k);
            assertEquals(86L, (long)v);
            return null;
        };
        assertNull(map.computeIfPresent(two, twoStep(b, f, g)));
        assertFalse(map.containsKey(two));
        assertTrue(b.get());

        map.put(two, 42L);
        f = (k, v) -> { map.put(two, 86L); return val; };
        g = (k, v) -> {
            assertSame(two, k);
            assertEquals(86L, (long)v);
            return 99L;
        };
        assertEquals(99L, (long)map.computeIfPresent(two, twoStep(b, f, g)));
        assertTrue(map.containsKey(two));
        assertTrue(b.get());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:42,代码来源:Defaults.java

示例4: testConcurrentMap_merge_racy

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
 * Simulates races by modifying the map within the remapping function.
 */
@Test
public void testConcurrentMap_merge_racy() {
    final AtomicBoolean b = new AtomicBoolean(true);
    final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
    final Long two = 2L;
    BiFunction<Long,Long,Long> f, g;

    for (Long val : new Long[] { null, 86L }) {
        map.clear();

        f = (v, w) -> { throw new AssertionError(); };
        assertEquals(99L, (long)map.merge(two, 99L, f));
        assertEquals(99L, (long)map.get(two));

        f = (v, w) -> { map.put(two, 42L); return val; };
        g = (v, w) -> {
            assertEquals(42L, (long)v);
            assertEquals(3L, (long)w);
            return v + w;
        };
        assertEquals(45L, (long)map.merge(two, 3L, twoStep(b, f, g)));
        assertEquals(45L, (long)map.get(two));
        assertTrue(b.get());

        f = (v, w) -> { map.remove(two); return val; };
        g = (k, v) -> { throw new AssertionError(); };
        assertEquals(55L, (long)map.merge(two, 55L, twoStep(b, f, g)));
        assertEquals(55L, (long)map.get(two));
        assertTrue(map.containsKey(two));
        assertFalse(b.get()); b.set(true);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:Defaults.java

示例5: testStatsNoops

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void testStatsNoops() {
  CacheBuilder<Object, Object> builder = createCacheBuilder()
      .concurrencyLevel(1);
  LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader());
  ConcurrentMap<Object, Object> map = cache.localCache; // modifiable map view
  assertEquals(EMPTY_STATS, cache.stats());

  Object one = new Object();
  assertNull(map.put(one, one));
  assertSame(one, map.get(one));
  assertTrue(map.containsKey(one));
  assertTrue(map.containsValue(one));
  Object two = new Object();
  assertSame(one, map.replace(one, two));
  assertTrue(map.containsKey(one));
  assertFalse(map.containsValue(one));
  Object three = new Object();
  assertTrue(map.replace(one, two, three));
  assertTrue(map.remove(one, three));
  assertFalse(map.containsKey(one));
  assertFalse(map.containsValue(one));
  assertNull(map.putIfAbsent(two, three));
  assertSame(three, map.remove(two));
  assertNull(map.put(three, one));
  assertNull(map.put(one, two));

  assertThat(map).containsEntry(three, one);
  assertThat(map).containsEntry(one, two);

  //TODO(cgruber): Confirm with [email protected] that this is a reasonable substitute.
  //Set<Map.Entry<Object, Object>> entries = map.entrySet();
  //assertThat(entries).containsExactly(
  //    Maps.immutableEntry(three, one), Maps.immutableEntry(one, two));
  //Set<Object> keys = map.keySet();
  //assertThat(keys).containsExactly(one, three);
  //Collection<Object> values = map.values();
  //assertThat(values).containsExactly(one, two);

  map.clear();

  assertEquals(EMPTY_STATS, cache.stats());
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:43,代码来源:LocalLoadingCacheTest.java

示例6: testAsMap

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void testAsMap() {
  CacheBuilder<Object, Object> builder = createCacheBuilder();
  LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader());
  assertEquals(EMPTY_STATS, cache.stats());

  Object one = new Object();
  Object two = new Object();
  Object three = new Object();

  ConcurrentMap<Object, Object> map = cache.asMap();
  assertNull(map.put(one, two));
  assertSame(two, map.get(one));
  map.putAll(ImmutableMap.of(two, three));
  assertSame(three, map.get(two));
  assertSame(two, map.putIfAbsent(one, three));
  assertSame(two, map.get(one));
  assertNull(map.putIfAbsent(three, one));
  assertSame(one, map.get(three));
  assertSame(two, map.replace(one, three));
  assertSame(three, map.get(one));
  assertFalse(map.replace(one, two, three));
  assertSame(three, map.get(one));
  assertTrue(map.replace(one, three, two));
  assertSame(two, map.get(one));
  assertEquals(3, map.size());

  map.clear();
  assertTrue(map.isEmpty());
  assertEquals(0, map.size());

  cache.getUnchecked(one);
  assertEquals(1, map.size());
  assertSame(one, map.get(one));
  assertTrue(map.containsKey(one));
  assertTrue(map.containsValue(one));
  assertSame(one, map.remove(one));
  assertEquals(0, map.size());

  cache.getUnchecked(one);
  assertEquals(1, map.size());
  assertFalse(map.remove(one, two));
  assertTrue(map.remove(one, one));
  assertEquals(0, map.size());

  cache.getUnchecked(one);
  Map<Object, Object> newMap = ImmutableMap.of(one, one);
  assertEquals(newMap, map);
  assertEquals(newMap.entrySet(), map.entrySet());
  assertEquals(newMap.keySet(), map.keySet());
  Set<Object> expectedValues = ImmutableSet.of(one);
  Set<Object> actualValues = ImmutableSet.copyOf(map.values());
  assertEquals(expectedValues, actualValues);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:54,代码来源:LocalLoadingCacheTest.java

示例7: execute

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void execute(List<String> logFiles) {

        calc.start();

        saveProcessingInfo("\n*START HEAP LOG SCANNING...");

        resetResult();

        precompile(rgxList);

        ConcurrentMap<String, List<String>> rgxResult = new ConcurrentHashMap<>();

        for (int i = 0; i < logFiles.size(); i++) {
            try (RgxReader reader = new RgxReader(logFiles.get(i), Constants.logsCharset, Constants.logEventsCount)) {

                ArrayList<String> result;
                ConcurrentMap<String, List<String>> mapLogs;
                while (reader.next()) {

                    result = reader.getResult();
                    inSize += result.size();

                    mapLogs = mapLogs(result, logFiles.get(i));

                    outSize += mapLogs.
                            entrySet().
                            stream().
                            mapToInt(n -> n.getValue().size()).
                            sum();

                    reduceLogs(mapLogs, rgxResult);

                    mapLogs.clear();
                }
            } catch (IOException e) {
                ExcpReporting.LogError(this.getClass(), e);
            }

            mapped += outSize;

            saveProcessingInfo(String.format("in: %s, out: %s, %s", inSize, outSize, logFiles.get(i)));
            inSize = 0;
            outSize = 0;
        }

        // later need to estimate effect after applying trimToSize() method to ArrayList instance
        sortLogs(rgxResult);

        reduced += rgxResult.
                entrySet().
                stream().
                mapToInt(n -> n.getValue().size()).
                sum();

        saveFinalInfo(rgxResult);
        selector.setResult(rgxResult);

        calc.end();
    }
 
开发者ID:ripreal,项目名称:V8LogScanner,代码行数:60,代码来源:HeapOp.java

示例8: testConcurrentMap_compute_racy

import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
 * Simulates races by modifying the map within the remapping function.
 */
@Test
public void testConcurrentMap_compute_racy() {
    final AtomicBoolean b = new AtomicBoolean(true);
    final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
    final Long two = 2L;
    BiFunction<Long,Long,Long> f, g;

    // null -> null is a no-op; race not detected
    f = (k, v) -> { map.put(two, 42L); return null; };
    assertNull(map.compute(two, f));
    assertEquals(42L, (long)map.get(two));

    for (Long val : new Long[] { null, 86L }) {
        map.clear();

        f = (k, v) -> { map.put(two, 42L); return 86L; };
        g = (k, v) -> {
            assertSame(two, k);
            assertEquals(42L, (long)v);
            return k + v;
        };
        assertEquals(44L, (long)map.compute(two, twoStep(b, f, g)));
        assertEquals(44L, (long)map.get(two));
        assertTrue(b.get());

        f = (k, v) -> { map.remove(two); return val; };
        g = (k, v) -> {
            assertSame(two, k);
            assertNull(v);
            return 44L;
        };
        assertEquals(44L, (long)map.compute(two, twoStep(b, f, g)));
        assertEquals(44L, (long)map.get(two));
        assertTrue(map.containsKey(two));
        assertTrue(b.get());

        f = (k, v) -> { map.remove(two); return val; };
        g = (k, v) -> {
            assertSame(two, k);
            assertNull(v);
            return null;
        };
        assertNull(map.compute(two, twoStep(b, f, g)));
        assertNull(map.get(two));
        assertFalse(map.containsKey(two));
        assertTrue(b.get());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:52,代码来源:Defaults.java


注:本文中的java.util.concurrent.ConcurrentMap.clear方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。