KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > T1528rc3


1
2 class T1528rc3 extends Thread JavaDoc {
3     private static int i = 0;
4     public static void main(String JavaDoc[] args) {
5        synchronized ("foo" + "bar") {
6            // did we sync on the same object? If the compiler correctly
7
// inlined the String expression, and the JVM correctly
8
// does mutual exclusion, then we have already locked
9
// "foobar", stopping the second thread in its tracks.
10
System.out.print("1 ");
11            new T1528rc3().start(); // build new thread
12
try {
13                sleep(500); // give second thread a chance
14
} catch (InterruptedException JavaDoc e) {
15            }
16            if (i == 2)
17            System.out.println(" Constant not inlined; wrong object locked ");
18            else if (i == 0)
19            System.out.println(" Indeterminate - thread never ran ");
20            else if (i == 1)
21            System.out.print("3 ");
22            else
23            System.out.println(" Unexpected result ");
24        }
25     }
26     public void run() {
27        System.out.print("2 ");
28        i = 1;
29        synchronized ("foobar") {
30            i = 2;
31        }
32        System.out.print("4");
33     }
34 }
35     
Popular Tags