what's thread state after it goes out of wait set but cannot win the competition for lock ? the following code gives different answer on Solaris and Windows XP:
class State {
public static void main(String[] args) throws Exception {
Thread[] ta = new Thread[10];
for (int i=0; i<10; i++) {
ta[i] = new Thread() {
public void run() {
synchronized(Object.class) {
System.out.println(Thread.currentThread().getName() + " enter");
try {
Object.class.wait();
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " get lock");
for(;;);
}
}
};
ta[i].start();
}
Thread.sleep(3000);
synchronized(Object.class) {
Object.class.notifyAll();
}
Thread.sleep(3000);
for(int i = 0; i< ta.length; i++) {
System.out.println(ta[i].getName() + " : " + ta[i].getState());
}
}
}
On both Solaris and Windows, only one thread is RUNNABLE after notifyAll() is called. but on Solaris, the states of other threads are all WAITING while on Windows, the states are all BLOCKED. As a result, use code depending on Thread.State will not write once, run anywhere. it will has different behaviour on different platforms.