Created
March 17, 2019 08:48
-
-
Save honwhy/8236424d303ce9ec1740f685c8d90721 to your computer and use it in GitHub Desktop.
MultiThreadHashMap
This file contains 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
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