Synchronizing Threads

The major problem in designing a multithreaded language is preventing collisions in which more than one thread attempts to modify the same object at the same time. Naturally, the CPU cannot actually do two things at once, but one thread might be halted in mid-calculation and another allowed to use the same data, resulting in a disaster.

Java provides the foundation for solving this problem in the Object class, the ultimate parent of all Java objects. Each object has an associated lock variable that can be manipulated only by the JVM. This lock provides a monitor mechanism that can be used to allow only one thread at a time to have access to an object.

In various Java books, “lock” and “monitor” are used interchangeably. ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) 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.