Java Concurrency - Synchronized method

Java Conceptuel Diagram

About

Synchronized method

Syntax

To make a method synchronized, simply add the synchronized keyword to its declaration:

public synchronized void increment() {
      c++;
}

Process

A synchronized method:

  • performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed.
  • It locks:
    • If the method is an instance method, the monitor associated with the instance for which it was invoked.
    • If the method is static, the monitor associated with the Class object that represents the class in which the method is defined.
  • unlock the same monitor if the method's body is ever completed, either normally or abruptly.

Effect

When a thread invokes a synchronized method, it automatically acquires a class level lock for that method's object and releases it when the method returns. The lock release occurs even if the return was caused by an uncaught exception.

Synchronized Method has two effects:

  • It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  • when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

Type

Constructor

constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.

When constructing an object that will be shared between threads, be very careful that a reference to the object does not “leak” prematurely.

For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the following line to your constructor:

instances.add(this);

But then other threads can use instances to access the object before construction of the object is complete.

Documentation / Reference





Discover More
Data System Architecture
Concurrency - Guarded block (Thread Communication Idiom)

The most common coordination idiom between thread is the guarded block. Such a block begins by polling a condition that must be true before the block can proceed. The loop is wasteful, since it...
Data System Architecture
Concurrency - Starvation

Starvation is a forms of thread contention. Starvation describes a situation where a thread is unable : to gain regular access to shared resources to make progress. This happens when shared resources...
Java Conceptuel Diagram
Java Concurrency - Atomic Variable Access

in java The reads and writes operations are atomic for reference variables for most primitive variables (all types except long and double). all variables declared volatile (including long...
Java Conceptuel Diagram
Java Concurrency - Fine-grained synchronization

Synchronized statements are also useful for improving concurrency with fine-grained synchronization. Suppose: class MsLunch has two instance fields, c1 and c2, c1 and c2 are never used together....
Java Conceptuel Diagram
Java Concurrency - Lock Objects (java.util.concurrent.locks)

java/util/concurrent/locks/package-summaryLock Object package is a High Level Concurrency of the java.util.concurrent. For low-level, intrinsic lock (monitor), see . There is interfaces and classes that...
Java Conceptuel Diagram
Java Concurrency - Monitor (Intrinsic|Implicit) Lock

Synchronization is built around an internal object property known as: intrinsic lock monitor lock implicit lock or simply monitor. Each object in Java is associated with a monitor, which a thread...
Java Conceptuel Diagram
Java Concurrency - Synchronization (Thread Safety)

in java. Java offers two basic synchronization idioms: synchronized methods synchronized statements final fields, which cannot be modified after the object is constructed, can be safely read...
Java Conceptuel Diagram
Java Concurrency - Synchronized Statement

Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock. The synchronized...



Share this page:
Follow us:
Task Runner