1 package org.opendaylight.yangtools.triemap;
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.LinkedList;
7 import java.util.Map.Entry;
8 import java.util.concurrent.ConcurrentHashMap;
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors;
11 import java.util.concurrent.TimeUnit;
12 import org.junit.Test;
14 public class TestMultiThreadMapIterator {
15 private static final int NTHREADS = 7;
18 public void testMultiThreadMapIterator () {
19 final Map<Object, Object> bt = new TrieMap<> ();
20 for (int j = 0; j < 50 * 1000; j++) {
21 final Object[] objects = getObjects (j);
22 for (final Object o : objects) {
27 System.out.println ("Size of initialized map is " + bt.size ());
30 final ExecutorService es = Executors.newFixedThreadPool (NTHREADS);
31 for (int i = 0; i < NTHREADS; i++) {
32 final int threadNo = i;
33 es.execute (new Runnable () {
36 for (Entry<Object, Object> e : bt.entrySet ()) {
37 if (accepts (threadNo, NTHREADS, e.getKey ())) {
38 String newValue = "TEST:" + threadNo;
39 e.setValue (newValue);
48 es.awaitTermination (3600L, TimeUnit.SECONDS);
49 } catch (final InterruptedException e) {
55 for (final Map.Entry<Object, Object> kv : bt.entrySet ()) {
56 Object value = kv.getValue ();
57 TestHelper.assertTrue (value instanceof String);
60 TestHelper.assertEquals (50000 + 2000 + 1000 + 100, count);
62 final ConcurrentHashMap<Object, Object> removed = new ConcurrentHashMap<> ();
65 final ExecutorService es = Executors.newFixedThreadPool (NTHREADS);
66 for (int i = 0; i < NTHREADS; i++) {
67 final int threadNo = i;
68 es.execute (new Runnable () {
71 for (final Iterator<Map.Entry<Object, Object>> i = bt.entrySet ().iterator (); i.hasNext ();) {
72 final Entry<Object, Object> e = i.next ();
73 Object key = e.getKey ();
74 if (accepts (threadNo, NTHREADS, key)) {
75 if (null == bt.get (key)) {
76 System.out.println (key);
79 if (null != bt.get (key)) {
80 System.out.println (key);
82 removed.put (key, key);
91 es.awaitTermination (3600L, TimeUnit.SECONDS);
92 } catch (final InterruptedException e) {
98 for (final Object value : bt.keySet ()) {
102 for (final Object o : bt.keySet ()) {
103 if (!removed.contains (bt.get (o))) {
104 System.out.println ("Not removed: " + o);
107 TestHelper.assertEquals (0, count);
108 TestHelper.assertEquals (0, bt.size ());
109 TestHelper.assertTrue (bt.isEmpty ());
112 protected static boolean accepts (final int threadNo, final int nThreads, final Object key) {
113 int val = getKeyValue (key);
115 return val % nThreads == threadNo;
121 private static int getKeyValue (final Object key) {
123 if (key instanceof Integer) {
124 val = ((Integer) key).intValue ();
126 else if (key instanceof Character) {
127 val = Math.abs (Character.getNumericValue ((Character) key) + 1);
129 else if (key instanceof Short) {
130 val = ((Short) key).intValue () + 2;
132 else if (key instanceof Byte) {
133 val = ((Byte) key).intValue () + 3;
140 static Object[] getObjects (final int j) {
141 final Collection<Object> results = new LinkedList<> ();
142 results.add (Integer.valueOf (j));
144 results.add (Character.valueOf ((char) j));
147 results.add (Short.valueOf ((short) j));
150 results.add (Byte.valueOf ((byte) j));
153 return results.toArray ();