O'Reilly logo

Java™ Puzzlers: Traps, Pitfalls, and Corner Cases by Neal Gafter, Joshua Bloch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required