I discovered that using
synchronized blocks in code compiled w/ SDK1.4 compiler generates byte code that won't run under JDK1.2. Specifically, 1.2 VM throws a
java.lang.IllegalMonitorStateException and terminates execution of my program when it encounters a synchronized code block. . .
YIKES!
Has anyone has had similar experience(s)? (Please post here) This is a real problem for my company since our products need to be binary compatibile back to JDK 1.2.x.
Chris
San Mateo, CA
PS -- I submitted this apparent bug to Sun yesterday.
========================
MY ENVIRONMENT
**1.4 environment** (compile code w/ javac.exe)
C:\jdk1.4.0\bin\java -version
java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)
**1.2 environment** (execute bytecode w/ java.exe)
C:\jdk1.2.2\bin\java -version
java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)
========================
You can reproduce the behavior by compiling the code in Merlin's javac:
C:\jdk1.4.0\bin>javac C:\Test\TestSynchronizedBlock.java
and running the bytecode in JDK1.2:
C:\Test>C:\jdk1.2.2\bin\java TestSynchronizedBlock
Example source code:
============== BEGIN CODE ==============
public class TestSynchronizedBlock
{
public TestSynchronizedBlock()
{
super();
synchronized(this)
{
System.out.println("Hello World!");
}
}
public static void main(java.lang.String[] args)
{
TestSynchronizedBlock test = new TestSynchronizedBlock();
}
}
============== END OF CODE ==============
Will result in output like this:
Hello World!
Exception in thread "main" java.lang.IllegalMonitorStateException: current thread not owner
at TestSynchronizedBlock.main(TestSynchronizedBlock.java:16)
Workaround: You can use a static method instead, which seems to run ok -- try something like this:
============== BEGIN CODE ==============
public class TestSynchronizedBlock2
{
public TestSynchronizedBlock2()
{
super();
//synchronized(this)
{
// call synchronized method instead
SynchronizedMethod();
}
}
synchronized void SynchronizedMethod()
{
System.out.println("Hello World!");
}
public static void main(java.lang.String[] args)
{
TestSynchronizedBlock2 test = new TestSynchronizedBlock2();
}
}
============== END OF CODE ==============