/** Make a new empty {@link NonBlockingHashSet}. */ public NonBlockingHashSet() { super(); _map = new NonBlockingHashMap<E,Object>(); }
private void createImplementation(ThreadParams threads) { if ("ConcurrentHashMap".equalsIgnoreCase(implementation)) { map = new ConcurrentHashMap<String, String>(16, 0.75f, 16); } else if ("NonBlockingHashMap".equalsIgnoreCase(implementation)) { map = new NonBlockingHashMap<String, String>(); } else if ("HashMap".equalsIgnoreCase(implementation)){ map = threads.getGroupCount() == 1 ? new HashMap<String, String>() : Collections.synchronizedMap(new HashMap<String, String>()); } else{ throw new IllegalArgumentException("Unsupported map: " + implementation); } }
/** Removes all of the mappings from this map. */ @Override public void clear() { // Smack a new empty table down Object[] newkvs = new NonBlockingHashMap(MIN_SIZE)._kvs; while( !CAS_kvs(_kvs,newkvs) ) // Spin until the clear works ; }
static ConcurrentMap<String, String> make_map(int impl) { switch (impl) { case 1: return null; // new Hashtable<String,String>(0); case 2: return null; // new CliffWrapHerlihy(); // was a non-blocking HashSet implementation from Maurice // Herlihy case 3: return new ConcurrentHashMap<String, String>(16, 0.75f, 16); // force to 16 striping case 4: return new ConcurrentHashMap<String, String>(16, 0.75f, 256); // force to 256 striping case 5: return new ConcurrentHashMap<String, String>(16, 0.75f, 4096); // force to 4096 striping case 6: return new NonBlockingHashMap<String, String>(); default: throw new Error("Bad imple"); } }
@Override protected Map<String, String> create(Map.Entry<String, String>[] entries) { Map<String, String> map = new NonBlockingHashMap<>(); for (Map.Entry<String, String> entry : entries) { map.put(entry.getKey(), entry.getValue()); } return map; } }, NonBlockingHashMap.class.getSimpleName());
@BeforeClass public static void setUp() { _nbhm = new NonBlockingHashMap<>(); }
public NonBlockingHashMap<Long, TestKey> getMapMultithreaded() throws InterruptedException, ExecutionException { final int threadCount = _items.keySet().size(); final NonBlockingHashMap<Long, TestKey> map = new NonBlockingHashMap<>(); // use a barrier to open the gate for all threads at once to avoid rolling start and no actual concurrency final CyclicBarrier barrier = new CyclicBarrier(threadCount); final ExecutorService ex = Executors.newFixedThreadPool(threadCount); final CompletionService<Integer> co = new ExecutorCompletionService<>(ex); for (Integer type : _items.keySet()) { // A linked-list of things to insert List<TestKey> items = _items.get(type); TestKeyFeederThread feeder = new TestKeyFeederThread(items, map, barrier); co.submit(feeder); } // wait for all threads to return int itemCount = 0; for (int retCount = 0; retCount < threadCount; retCount++) { final Future<Integer> result = co.take(); itemCount += result.get(); } ex.shutdown(); return map; } }
@Test public void putReturnValuesAreDistinct() throws Exception Map<String, Long> map = new NonBlockingHashMap<>(); map.put("K", -1l); int processors = Runtime.getRuntime().availableProcessors();
@Test public void testConcurrentSimple() throws InterruptedException final NonBlockingHashMap<String, String> nbhm = new NonBlockingHashMap<>();
@Test public void replaceResultIsBasedOnEquality() { NonBlockingHashMap<Integer, Integer> map = new NonBlockingHashMap<>(); Integer initialValue = new Integer(10); map.put(1, initialValue); assertTrue(map.replace(1, initialValue, 20)); assertTrue(map.replace(1, new Integer(20), 30)); }
@Test public void removeResultIsBasedOnEquality() { NonBlockingHashMap<Integer, Integer> map = new NonBlockingHashMap<>(); Integer initialValue = new Integer(10); map.put(1, initialValue); assertTrue(map.remove(1, initialValue)); map.put(1, initialValue); assertTrue(map.remove(1, new Integer(10))); }
final int ITEM_COUNT = PER_CNT * THREAD_COUNT; // fix roundoff for odd thread counts NonBlockingHashMap<Long, TestKey> nbhml = new NonBlockingHashMap<>();
@Test public void testIterationBig2() { final int CNT = 10000; NonBlockingHashMap<Integer, String> nbhm = new NonBlockingHashMap<>(); final String v = "v"; for (int i = 0; i < CNT; i++) { final Integer z = i; String s0 = nbhm.get(z); assertThat(s0, nullValue()); nbhm.put(z, v); String s1 = nbhm.get(z); assertThat(s1, is(v)); } assertThat(nbhm.size(), is(CNT)); }
@Test public final void testNonBlockingHashMapSize() NonBlockingHashMap<Long, String> items = new NonBlockingHashMap<>(); items.put(100L, "100"); items.put(101L, "101");
NonBlockingHashMap<KeyBonk, String> dumb = new NonBlockingHashMap<>(); for (int i = 0; i < 10000; i++) NonBlockingHashMap<Integer, Object> map = new NonBlockingHashMap<>(32); for (int i = 1; i < 32; i++)
/** Make a new empty {@link NonBlockingHashSet}. */ public NonBlockingHashSet() { super(); _map = new NonBlockingHashMap<E,Object>(); }
/** Removes all of the mappings from this map. */ @Override public void clear() { // Smack a new empty table down Object[] newkvs = new NonBlockingHashMap(MIN_SIZE)._kvs; while( !CAS_kvs(_kvs,newkvs) ) // Spin until the clear works ; }