private void add() { int i = 0; while (i < count) { Random rand = new Random(); int r = rand.nextInt(50); if (!hashMap.containsKey(r)) { hashMap.put(r, r); i++; } } }
private void remove() { int i = 0; while (i < count) { Random rand = new Random(); int r = rand.nextInt(50); if (hashMap.containsKey(r)) { hashMap.remove(r); i++; } } }
public void run() { if (id % 2 == 0) { add(); } else { remove(); } // size should include changes made by other threads // (should equal last printed size plus ADD(s) and minus REMOVE(s) System.out.println("size: " + hashMap.size()); }