Skip to content

Instantly share code, notes, and snippets.

@honwhy
Created March 17, 2019 08:48
Show Gist options
  • Save honwhy/8236424d303ce9ec1740f685c8d90721 to your computer and use it in GitHub Desktop.
Save honwhy/8236424d303ce9ec1740f685c8d90721 to your computer and use it in GitHub Desktop.
MultiThreadHashMap
public class MultiThreadHashMap {
private static final Object PRESENT = new Object();
public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
final Map<String,Object> map = new HashMap<>(); //let map do resize multiple times
int N = 1000;
final CyclicBarrier cyclicBarrier = new CyclicBarrier(N+1);
for (int i = 0; i < N; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
//e.printStackTrace();
} catch (BrokenBarrierException e) {
//e.printStackTrace();
}
System.out.println("about to put value");
map.put(UUID.randomUUID().toString().replace("-",""), PRESENT);
}
}).start();
}
cyclicBarrier.await();
System.out.println("about to finish");
Thread.sleep(2000L);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment