home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Java Language Reference

Previous Chapter 6
Statements and Control Structures

6.13 The synchronized Statement

A synchronized statement provides a way of synchronizing the execution of a block, so that only one thread can be executing the block at a time:

[Graphic: Figure from the text]

The expression in parentheses must produce a reference type, or the compiler issues an error message. If the expression evaluates to null, a NullPointerException is thrown.

Before executing the block in a synchronized statement, the current thread obtains a lock for the object referenced by the expression. While the block is being executed, no other thread can obtain the lock for that object. When the thread is done executing the block, it releases the lock, so it is available for other threads. See Chapter 8 for a complete discussion of threads in Java.

References Blocks; Expression 4; Runtime exceptions; Threads 8

Previous Home Next
The try Statement Book Index Program Structure

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java