-
Bug
-
Resolution: Fixed
-
P4
-
7
-
b36
-
x86
-
linux
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8018938 | 7u45 | Sean Chou | P4 | Closed | Fixed | b01 |
JDK-2229322 | 7u40 | Sean Coffey | P4 | Closed | Fixed | b08 |
FULL PRODUCT VERSION :
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Client VM (build 21.1-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
OS independent, all linux, solaris and windows are affected.
A DESCRIPTION OF THE PROBLEM :
AbstractCollection.toArray(T[] ) 's behavior maybe different from the spec in multithread environment. The spec says "If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection." However, in multithread environment, it is not easy to tell if the collection fits in the specified array, because the items may be removed when toArray is copying. The current implementation get the size before copying the items, so the returned array may fits into the specified array but a new array is returned.
There are more detail on mailinglist:
http://mail.openjdk.java.net/pipermail/core-libs-dev/2011-December/008686.html
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the testcase
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The testcase prints : Is buffer used : true
ACTUAL -
The testcase prints : Is buffer used : false
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class CollectionToArrayTest {
static volatile Map<String, String> map = new TConcurrentHashMap<String, String>();
static volatile boolean gosleep = true;
static class TConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
public int size() {
int oldresult = super.size();
System.out.println("map size before concurrent remove is "
+ oldresult);
while (gosleep) {
try {
// Make sure the map is modified during toArray is called,
// between getsize and being iterated.
Thread.sleep(1000);
// System.out.println("size called, size is " + oldresult +
// " take a sleep to make sure the element is deleted before size is returned.");
} catch (Exception e) {
}
}
return oldresult;
}
}
static class ToArrayThread implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
String str = Integer.toString(i);
map.put(str, str);
}
String[] buffer = new String[4];
String[] strings = map.values().toArray(buffer);
// System.out.println("length is " + strings.length);
if (strings.length <= buffer.length) {
System.out.println("given array size is "
+ buffer.length
+ " \nreturned array size is "
+ strings.length
+ ", \nbuffer should be used according to spec. Is buffer used : "
+ (strings == buffer));
}
}
}
static class RemoveThread implements Runnable {
public void run() {
String str = Integer.toString(0);
map.remove(str);
gosleep = false;
}
}
public static void main(String args[]) {
CollectionToArrayTest app = new CollectionToArrayTest();
app.test_concurrentRemove();
}
public void test_concurrentRemove() {
System.out.println("//////////////////////////////////////////////\n" +
"The spec says if the given array is large\n " +
"enough to hold all elements, the given array\n" +
"should be returned by toArray. This \n" +
"testcase checks this case. \n" +
"//////////////////////////////////////////////");
Thread[] threads = new Thread[2];
threads[0] = new Thread(new ToArrayThread());
threads[1] = new Thread(new RemoveThread());
threads[0].start();
try {
// Take a sleep to make sure toArray is already called.
Thread.sleep(1200);
} catch (Exception e) {
}
threads[1].start();
}
}
---------- END SOURCE ----------
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Client VM (build 21.1-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
OS independent, all linux, solaris and windows are affected.
A DESCRIPTION OF THE PROBLEM :
AbstractCollection.toArray(T[] ) 's behavior maybe different from the spec in multithread environment. The spec says "If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection." However, in multithread environment, it is not easy to tell if the collection fits in the specified array, because the items may be removed when toArray is copying. The current implementation get the size before copying the items, so the returned array may fits into the specified array but a new array is returned.
There are more detail on mailinglist:
http://mail.openjdk.java.net/pipermail/core-libs-dev/2011-December/008686.html
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the testcase
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The testcase prints : Is buffer used : true
ACTUAL -
The testcase prints : Is buffer used : false
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class CollectionToArrayTest {
static volatile Map<String, String> map = new TConcurrentHashMap<String, String>();
static volatile boolean gosleep = true;
static class TConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
public int size() {
int oldresult = super.size();
System.out.println("map size before concurrent remove is "
+ oldresult);
while (gosleep) {
try {
// Make sure the map is modified during toArray is called,
// between getsize and being iterated.
Thread.sleep(1000);
// System.out.println("size called, size is " + oldresult +
// " take a sleep to make sure the element is deleted before size is returned.");
} catch (Exception e) {
}
}
return oldresult;
}
}
static class ToArrayThread implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
String str = Integer.toString(i);
map.put(str, str);
}
String[] buffer = new String[4];
String[] strings = map.values().toArray(buffer);
// System.out.println("length is " + strings.length);
if (strings.length <= buffer.length) {
System.out.println("given array size is "
+ buffer.length
+ " \nreturned array size is "
+ strings.length
+ ", \nbuffer should be used according to spec. Is buffer used : "
+ (strings == buffer));
}
}
}
static class RemoveThread implements Runnable {
public void run() {
String str = Integer.toString(0);
map.remove(str);
gosleep = false;
}
}
public static void main(String args[]) {
CollectionToArrayTest app = new CollectionToArrayTest();
app.test_concurrentRemove();
}
public void test_concurrentRemove() {
System.out.println("//////////////////////////////////////////////\n" +
"The spec says if the given array is large\n " +
"enough to hold all elements, the given array\n" +
"should be returned by toArray. This \n" +
"testcase checks this case. \n" +
"//////////////////////////////////////////////");
Thread[] threads = new Thread[2];
threads[0] = new Thread(new ToArrayThread());
threads[1] = new Thread(new RemoveThread());
threads[0].start();
try {
// Take a sleep to make sure toArray is already called.
Thread.sleep(1200);
} catch (Exception e) {
}
threads[1].start();
}
}
---------- END SOURCE ----------
- backported by
-
JDK-2229322 (coll) Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
-
- Closed
-
-
JDK-8018938 (coll) Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
-
- Closed
-