Skip to content

8343426: ConcurrentSkipListMap.spliterator() can no longer split the stream #3664

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3193,14 +3193,14 @@ public final Comparator<? super K> getComparator() {
}
// factory method for KeySpliterator
final KeySpliterator<K,V> keySpliterator() {
Index<K,V> h; Node<K,V> n; long est;
Index<K,V> h; Node<K,V> hn, n; long est;
VarHandle.acquireFence();
if ((h = head) == null) {
if ((h = head) == null || (hn = h.node) == null) {
n = null;
est = 0L;
}
else {
n = h.node;
n = hn.next;
est = getAdderCount();
}
return new KeySpliterator<K,V>(comparator, h, n, null, est);
Expand Down Expand Up @@ -3279,14 +3279,14 @@ public int characteristics() {

// Almost the same as keySpliterator()
final ValueSpliterator<K,V> valueSpliterator() {
Index<K,V> h; Node<K,V> n; long est;
Index<K,V> h; Node<K,V> hn, n; long est;
VarHandle.acquireFence();
if ((h = head) == null) {
if ((h = head) == null || (hn = h.node) == null) {
n = null;
est = 0L;
}
else {
n = h.node;
n = hn.next;
est = getAdderCount();
}
return new ValueSpliterator<K,V>(comparator, h, n, null, est);
Expand Down Expand Up @@ -3383,14 +3383,14 @@ public final Comparator<Map.Entry<K,V>> getComparator() {

// Almost the same as keySpliterator()
final EntrySpliterator<K,V> entrySpliterator() {
Index<K,V> h; Node<K,V> n; long est;
Index<K,V> h; Node<K,V> hn, n; long est;
VarHandle.acquireFence();
if ((h = head) == null) {
if ((h = head) == null || (hn = h.node) == null) {
n = null;
est = 0L;
}
else {
n = h.node;
n = hn.next;
est = getAdderCount();
}
return new EntrySpliterator<K,V>(comparator, h, n, null, est);
Expand Down
31 changes: 31 additions & 0 deletions test/jdk/java/util/concurrent/tck/ConcurrentSkipListMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,37 @@ private int lastAscending() {
}
}

/**
* Spliterators.trySplit() returns null with an empty map and non-null when
* it is not empty.
*/
public void testSpliterators() {
// To test JDK-8343426
ConcurrentSkipListMap<Integer, Integer> map = new ConcurrentSkipListMap<>();
for (int i = 1; i <= 1000; i++) map.put(i, i);
// ensure that the splits do happen
assertNotNull(map.keySet().spliterator().trySplit());
assertNotNull(map.entrySet().spliterator().trySplit());
assertNotNull(map.values().spliterator().trySplit());
// ensure that the splits return *all* the items in the set
assertEquals(500_500, map.keySet()
.parallelStream()
.mapToInt(Integer::valueOf)
.sum());
assertEquals(500_500, map.values()
.parallelStream()
.mapToInt(Integer::valueOf)
.sum());
assertEquals(500_500 * 2, map.entrySet()
.parallelStream()
.mapToInt(entry -> entry.getKey() + entry.getValue())
.sum());
map.clear();
assertNull(map.keySet().spliterator().trySplit());
assertNull(map.entrySet().spliterator().trySplit());
assertNull(map.values().spliterator().trySplit());
}

static void assertEq(Item i, int j) {
if (i == null)
mustEqual(j, -1);
Expand Down