FULL PRODUCT VERSION :
[~]$ java -version
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)
FULL OS VERSION :
[test]$ uname -a
Linux zinad 2.6.18-4-k7 #1 SMP Mon Mar 26 17:57:15 UTC 2007 i686 GNU/Linux
EXTRA RELEVANT SYSTEM CONFIGURATION :
proc: AthlonXP 2000+
A DESCRIPTION OF THE PROBLEM :
The following code contains two //-------\\ lines between which Thread.sleep() is called.
If that call is commented out, the average for the time reported is 1076 milliseconds.
If uncommented, the average for the time reported is 52301 milliseconds. I also encountered two cases in which the process had to be killed not returning after aproximately 40 min of running.
for comparison, in Windows XP when commented – 1025 milliseconds, when uncommented – 1074 milliseconds.
The averages were computed from at least 10 results each.
THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No
THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: No
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac MainAndThread1.java
java MainAndThread1
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED: either the Thread.sleep() method called or not the two threads should have equivalent chances of aquiring the lock once the Thread2 object has exited method2().
ACTUAL: if the synchronized block calls Thread.sleep(), the thread that has the lock (Thread2) has more chances of aquiring the lock in the next step.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
//*****************************************************\import java.util.Date;
import java.util.concurrent.TimeUnit;
public class MainAndThread1 {
public static void main(String[] args) {
LockableObject lo = new LockableObject();
Date d1 = new Date();
new Thread(new Thread2(lo)).start();
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch(InterruptedException ignore) {}
lo.method1();
Date d2 = new Date();
System.out.println("It took: " + (d2.getTime() - d1.getTime()) + " milliseconds");
System.exit(0);
}
}
class Thread2 implements Runnable{
private LockableObject lo;
public Thread2(LockableObject lo) {
this.lo = lo;
}
public void run() {
while (true) {
lo.method2();
}
}
}
class LockableObject {
public synchronized void method1() {
System.out.println("method1");
}
public synchronized void method2() {
System.out.println("method2");
//-----------------------------------------------------------------------------------\ try {
TimeUnit.NANOSECONDS.sleep(5);
} catch(InterruptedException ignore) {}
//-----------------------------------------------------------------------------------\ }
}
//***************************************************************\---------- END SOURCE ----------
[~]$ java -version
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)
FULL OS VERSION :
[test]$ uname -a
Linux zinad 2.6.18-4-k7 #1 SMP Mon Mar 26 17:57:15 UTC 2007 i686 GNU/Linux
EXTRA RELEVANT SYSTEM CONFIGURATION :
proc: AthlonXP 2000+
A DESCRIPTION OF THE PROBLEM :
The following code contains two //-------\\ lines between which Thread.sleep() is called.
If that call is commented out, the average for the time reported is 1076 milliseconds.
If uncommented, the average for the time reported is 52301 milliseconds. I also encountered two cases in which the process had to be killed not returning after aproximately 40 min of running.
for comparison, in Windows XP when commented – 1025 milliseconds, when uncommented – 1074 milliseconds.
The averages were computed from at least 10 results each.
THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No
THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: No
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac MainAndThread1.java
java MainAndThread1
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED: either the Thread.sleep() method called or not the two threads should have equivalent chances of aquiring the lock once the Thread2 object has exited method2().
ACTUAL: if the synchronized block calls Thread.sleep(), the thread that has the lock (Thread2) has more chances of aquiring the lock in the next step.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
//*****************************************************\import java.util.Date;
import java.util.concurrent.TimeUnit;
public class MainAndThread1 {
public static void main(String[] args) {
LockableObject lo = new LockableObject();
Date d1 = new Date();
new Thread(new Thread2(lo)).start();
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch(InterruptedException ignore) {}
lo.method1();
Date d2 = new Date();
System.out.println("It took: " + (d2.getTime() - d1.getTime()) + " milliseconds");
System.exit(0);
}
}
class Thread2 implements Runnable{
private LockableObject lo;
public Thread2(LockableObject lo) {
this.lo = lo;
}
public void run() {
while (true) {
lo.method2();
}
}
}
class LockableObject {
public synchronized void method1() {
System.out.println("method1");
}
public synchronized void method2() {
System.out.println("method2");
//-----------------------------------------------------------------------------------\ try {
TimeUnit.NANOSECONDS.sleep(5);
} catch(InterruptedException ignore) {}
//-----------------------------------------------------------------------------------\ }
}
//***************************************************************\---------- END SOURCE ----------
- duplicates
-
JDK-4985566 Equal priority threads not timslicing properly in Linux
-
- Closed
-
- relates to
-
JDK-6316090 5.0 "synchronized" method causes starvation
-
- Closed
-
-
JDK-4813310 Map Thread priorities to system thread/process priorities
-
- Closed
-