synchronized keyword

Method syntax

>>-+-----------+-+--------+-+-------+-synchronized-+--------+->
   +-public----+ '-static-' '-final-'              '-native-'
   +-protected-+
   '-private---'

>-ResultType-Identifier-(-+---------------------+-)->
                          '-FormalParameterList-'

>-+----------------------+-MethodBody-><
  '-throws-ClassTypeList-'

Block syntax

>>-synchronized-(-Expression-)-Block-><

Description
The synchronized modifier is used when working with threads. A synchronized method acquires a lock before it executes. It automatically locks an object before executing its body and automatically unlocks the object on return, as if by use of a synchronized statement, thus allowing its activities to be synchronized with those of other threads. For a class method, the lock associated with the Class object for the method's class is used. For an instance method, the lock associated with this (the object for which the method was invoked) is used. These are the same locks that can be used by the synchronized statement.

A synchronized method automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed. If the method is an instance method, it locks the lock associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). If the method is static, it locks the lock associated with the Class object that represents the class in which the method is defined. If execution of the method's body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same lock.

A synchronized statement acquires a mutual-exclusion lock on behalf of the executing thread, executes a block, then releases the lock. While the executing thread owns the lock, no other thread may acquire the lock.

The type of Expression must be a reference type, or a compilation error occurs. A synchronized statement is executed by first evaluating the Expression. If evaluation of the Expression completes abruptly for some reason, then the synchronized statement completes abruptly for the same reason. If the value of the Expression is null, a NullPointerException is thrown.

Otherwise, suppose the non-null value of the Expression is V. The executing thread locks the lock associated with V. Then the block is executed. If execution of the block completes normally, then the lock is unlocked and the synchronized statement completes normally.  If execution of the block completes abruptly for any reason, then the lock is unlocked and the synchronized statement then completes abruptly for the same reason.

A constant declaration in an interface must not include the modifier synchronized or a compilation error occurs.

Example
The following example shows a simple synchronized method:

public class SynchronizedClass {
    private int thisFieldNeedsToBeThreadSafe = 99;

    public synchronized int whatsTheValue() {
        return thisFieldNeedsToBeThreadSafe++;
    }
}

This ensures that access to the thisFieldNeedsToBeThreadSafe variable is thread safe; only only one thread at a time can increment the field, no matter how many competing threads call the instance method.

The locks acquired by synchronized statements are the same as the locks that are acquired implicitly by synchronized methods. A single thread may hold a lock more than once. The following example:

class Test {
    public static void main(String[]args){
        Test t = new Test();
        synchronized(t) {
            System.out.println("made it!");
        }
    }
}

prints:

made it!

This example would result in a deadlock if a single thread were not permitted to lock a lock more than once.

ngrelr.gif (548 bytes)
Syntax diagrams
Java types
final keyword
native keyword
private keyword
protected keyword
public keyword
static keyword
throws keyword

Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.