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

IntSet.toIntArray() fails with ArrayIndexOutOfBoundsException in multi-threaded env #46

Open
AMilassin opened this issue Feb 11, 2016 · 0 comments

Comments

@AMilassin
Copy link

I have encountered an issue where in an incorrectly synchronized multi-threaded environment the IntSet.toIntArray() method call returns an ArrayIndexOutOfBoundsException.

This is not a major issue and it is acceptable as "undefined behavior" while accessing a non thread safe collection without proper guards but throwing a ConcurrentModificationException would be more helpful to find these problems.

Test code

@Test
public void toIntArray_should_not_throw_ArrayIndexOutOfBoundsException() throws Exception {
    final IntSet ids = HashIntSets.newUpdatableSet(1048576);
    final Random r = new Random();
    Thread  thread = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int j = 0; j < 2000000; j++) {
                ids.add(r.nextInt(1000000));
            }
        }
    });
    thread.start();

    for (int j = 0; j < 2000000; j++) {
        ids.add(r.nextInt(1000000));

        if (ids.size() % 1000 == 0) {
            int[] ints = ids.toIntArray();
            System.out.println(ints.length);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant