Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with toArray on value/keySet in ConcurrentHashMap #535

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions classpath/avian/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Collections;

public class Data {
public static int nextPowerOfTwo(int n) {
Expand All @@ -28,17 +27,20 @@ public static <V> boolean equal(V a, V b) {
return a == null ? b == null : a.equals(b);
}

public static <T> T[] toArray(Collection collection, T[] array) {
public static <T> T[] toArray(Collection<?> collection, T[] array) {
Class c = array.getClass().getComponentType();

if (array.length < collection.size()) {
int Csize = collection.size();
if (array.length < Csize) {
array = (T[]) java.lang.reflect.Array.newInstance(c, collection.size());
}

int i = 0;
for (Object o: collection) {
if (c.isInstance(o)) {
array[i++] = (T) o;
if(Csize <= i) {
break;
}
} else {
throw new ArrayStoreException();
}
Expand Down
29 changes: 29 additions & 0 deletions test/ConcurrentHashMapTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -13,6 +15,31 @@ private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}

private static void toArrayConcurrentMod() {
final AtomicBoolean run = new AtomicBoolean(true);
final Random RND = new Random();
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<Integer, Integer>();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(run.get()) {
map.put(RND.nextInt(), RND.nextInt());
while(map.size() > 500) {
for(Integer i: map.keySet()) {
map.remove(i);
break;
}
}
}
}});
t.setDaemon(true);
t.start();
for(int i=0; i<1000; i++) {
map.values().toArray();
}
run.set(false);
}

public static void main(String[] args) throws Throwable {
final ConcurrentMap<Integer, Object> map = new ConcurrentHashMap();
final int[] counter = new int[1];
Expand Down Expand Up @@ -103,6 +130,8 @@ public void run() {
map.notifyAll();
}
}

toArrayConcurrentMod();
}

private static void populateCommon(ConcurrentMap<Integer, Object> map) {
Expand Down