9. More Library Puzzlers

The puzzles in this chapter feature more advanced library topics, such as threading, reflection, and I/O.

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 ...

Get Java™ Puzzlers: Traps, Pitfalls, and Corner Cases now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.