Created
October 4, 2017 21:55
-
-
Save krisiye/b3a597b5e9a8e72904cc444eb1522979 to your computer and use it in GitHub Desktop.
Hasmap rehash code example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example with a hashmap to demonstrate how we could end up missing hashmap entries | |
// under concurrent modification. | |
package test; | |
import java.util.HashMap; | |
public class MapTestTask implements Runnable { | |
private static HashMap<Object, Object> hashMap; | |
private Object value = new Object(); | |
public MapTestTask(HashMap<Object, Object> map) { | |
this.hashMap = map; | |
} | |
public void run() { | |
hashMap.put(Thread.currentThread().getId(), value); | |
Object retrieved = hashMap.get(Thread.currentThread().getId()); | |
if (retrieved == null) { | |
// Can it ever Happen | |
System.out.println("just happened with key '" + Thread.currentThread().getId() + "'. " + hashMap); | |
System.exit(1); | |
} | |
else | |
{ | |
System.out.println("Looks good!"); | |
} | |
} | |
} | |
package test; | |
import java.util.HashMap; | |
public class TestMap { | |
public static void main(String[] args) throws InterruptedException { | |
HashMap<Object, Object> map = new HashMap<Object, Object>(); | |
int NUM_THREADS = 1000; | |
Thread[] threads = new Thread[NUM_THREADS]; | |
for (int i = 0; i < NUM_THREADS; i++) { | |
threads[i] = new Thread(new MapTestTask(map)); | |
} | |
for (int i = 0; i < NUM_THREADS; i++) { | |
threads[i].start(); | |
} | |
for (int i = 0; i < NUM_THREADS; i++) { | |
threads[i].join(); | |
} | |
} | |
// Fixes with a ConcurrentHashMap | |
package test; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class MapTestTask implements Runnable { | |
private static ConcurrentHashMap<Object, Object> hashMap; | |
private Object value = new Object(); | |
public MapTestTask(ConcurrentHashMap<Object, Object> map) { | |
this.hashMap = map; | |
} | |
public void run() { | |
hashMap.put(Thread.currentThread().getId(), value); | |
Object retrieved = hashMap.get(Thread.currentThread().getId()); | |
if (retrieved == null) { | |
// Can it ever Happen | |
System.out.println("just happened with key '" + Thread.currentThread().getId() + "'. " + hashMap); | |
System.exit(1); | |
} | |
else | |
{ | |
System.out.println("Looks good!"); | |
} | |
} | |
} | |
package test; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class TestMap { | |
public static void main(String[] args) throws InterruptedException { | |
ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<Object, Object>(); | |
int NUM_THREADS = 1000; | |
Thread[] threads = new Thread[NUM_THREADS]; | |
for (int i = 0; i < NUM_THREADS; i++) { | |
threads[i] = new Thread(new MapTestTask(map)); | |
} | |
for (int i = 0; i < NUM_THREADS; i++) { | |
threads[i].start(); | |
} | |
for (int i = 0; i < NUM_THREADS; i++) { | |
threads[i].join(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment