Skip to content

Instantly share code, notes, and snippets.

@rvprasad
Last active December 18, 2021 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvprasad/e573212b3f0247b5d4b42a5eeb2dce75 to your computer and use it in GitHub Desktop.
Save rvprasad/e573212b3f0247b5d4b42a5eeb2dce75 to your computer and use it in GitHub Desktop.
Illustrates the absence of thread safety between various methods of SychronizedSet and its iteraror method.
Collection<Integer> ss = Collections.synchronizedSet(new HashSet<Integer>());
new Thread() {
public void run() {
for (int x = 0; x < 1000; x++) {
ss.addAll(IntStream.range(x*1000, (x + 1)*1000)
.boxed()
.collect(Collectors.toList()));
}
}
}.start()
for (int x = 0; x < 10; x++) {
Collection<Integer> t = new HashSet<Integer>(ss);
// Fix
//Collections.addAll(new HashSet<Integer>(), ss.toArray(new Integer[0]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment