We have a partner company with the following problem
with "Lock Fairness" (see sample code below)
in the Solaris JVM (all versions from what they tell me).
I tried it in Windows JVM and it does exhibit fairness.
However, it does not in Solaris JVM.
public class TestWeirdness {
static class SpinThread implements Runnable {
SpinThread(Object lock) {
this.lock = lock;
}
public void run() {
System.err.println("SpinThread started");
int i = 0;
while (true) {
synchronized (lock) {
try {
Thread.sleep(500); System.err.println("loop " + i++);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private Object lock;
}
public static void main(String[] args) {
Object lock = new Object();
SpinThread st = new SpinThread(lock);
Thread t = new Thread(st);
t.start();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("Trying to acquire lock");
synchronized (lock) {
System.err.println("Acquired lock");
}
System.exit(0);
}
}
On Windohs (NT 4.0) and RedHat 6.2, I get
% java TestWeirdness
SpinThread started
loop 0
loop 1
Trying to acquire lock
loop 2
Acquired lock
On Solaris (I've tried 7, 8, 9) I get
% java TestWeirdness
SpinThread started
loop 0
loop 1
Trying to acquire lock
loop 2
loop 3
loop 4
loop 5
loop 6
loop 7
loop 8
loop 9
loop 10
and sometimes it eventually acquires the lock after dozens or
hundreds of iterations.
with "Lock Fairness" (see sample code below)
in the Solaris JVM (all versions from what they tell me).
I tried it in Windows JVM and it does exhibit fairness.
However, it does not in Solaris JVM.
public class TestWeirdness {
static class SpinThread implements Runnable {
SpinThread(Object lock) {
this.lock = lock;
}
public void run() {
System.err.println("SpinThread started");
int i = 0;
while (true) {
synchronized (lock) {
try {
Thread.sleep(500); System.err.println("loop " + i++);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private Object lock;
}
public static void main(String[] args) {
Object lock = new Object();
SpinThread st = new SpinThread(lock);
Thread t = new Thread(st);
t.start();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("Trying to acquire lock");
synchronized (lock) {
System.err.println("Acquired lock");
}
System.exit(0);
}
}
On Windohs (NT 4.0) and RedHat 6.2, I get
% java TestWeirdness
SpinThread started
loop 0
loop 1
Trying to acquire lock
loop 2
Acquired lock
On Solaris (I've tried 7, 8, 9) I get
% java TestWeirdness
SpinThread started
loop 0
loop 1
Trying to acquire lock
loop 2
loop 3
loop 4
loop 5
loop 6
loop 7
loop 8
loop 9
loop 10
and sometimes it eventually acquires the lock after dozens or
hundreds of iterations.