Java Reference
In-Depth Information
< Day Day Up >
Puzzle 76: Ping Pong
This program consists entirely of synchronized static methods. What does it print? Is it guaranteed
to print the same thing every time you run it?
public class PingPong {
public static synchronized void main(String[] a) {
Thread t = new Thread() {
public void run() { pong(); }
};
t.run();
System.out.print("Ping");
}
static synchronized void pong() {
System.out.print("Pong");
}
}
Solution 76: Ping Pong
In a multithreaded program, it is generally a good bet that the behavior can vary from run to run, but
this program always prints the same thing. Before a synchronized static method executes, it obtains
the monitor lock associated with its Class object [JLS 8.4.3.6]. Therefore, the main thread acquires
the lock on PingPong.class before creating the second thread. As long as the main thread holds on
to this lock, the second thread can't execute a synchronized static method. In particular, the second
thread can't execute the pong method until the main method prints Ping and completes execution.
Only then does the main thread relinquish the lock, allowing the second thread to acquire it and
print Pong . This analysis leaves little doubt that the program should always print PingPong . There is
one small problem: If you tried the program, you found that it always prints PongPing . What on
 
 
Search WWH ::




Custom Search