Skip to Main Content
Java™ Puzzlers: Traps, Pitfalls, and Corner Cases
book

Java™ Puzzlers: Traps, Pitfalls, and Corner Cases

by Joshua Bloch, Neal Gafter
June 2005
Beginner to intermediate content levelBeginner to intermediate
312 pages
6h 24m
English
Addison-Wesley Professional
Content preview from Java™ Puzzlers: Traps, Pitfalls, and Corner Cases

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition

A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition

Jay Wengrow
Algorithms, 4th Edition

Algorithms, 4th Edition

Robert Sedgewick, Kevin Wayne

Publisher Resources

ISBN: 032133678XPurchase book