Synchronization in Java

What Is a Thread?

In computer science, a thread is a sequence of instructions within a program that can be executed independently of other code. Many threads can run concurrently within a program. All Java programs have at least one thread, known as the main thread, which is created by the JVM at the program’s start, when the main() method is invoked with the main thread. It is an object that has its own registers, stack and code segment that can run parallel with other threads in a process (a process is a collection of threads).

What Is Multithreading?

Multithreading is a process of executing multiple threads simultaneously. That means, it is making use of two or more "threads" of execution, working together to accomplish a task. Each instruction sequence has its own unique flow of control that is independent of all others.

Synchronization in Java

In general, synchronization is used to protect access to resources that are accessed concurrently. One of the benefits of using multiple threads in an application is that each thread executes asynchronously. There are many situations in which multiple threads must share access to common objects . For example, in a database system, you might not want one thread to be updating a database record while another thread is trying to read it. In these types of cases, we need to ensure that resource will be used by only one thread at a time. Otherwise, two or more threads could access the same resource at the same time, each unaware of the other's actions. Java enables you to coordinate the actions of multiple threads using synchronized methods and synchronized statements . An object for which access is to be coordinated is accessed through the use of synchronized methods. These methods are declared with the synchronized keyword. Only one synchronized method can be invoked for an object at a given point in time. This keeps synchronized methods in multiple threads from conflicting with each other. Following is the general form of the synchronized statement: Syntax
synchronized(objectidentifier) { // Access shared variables and other shared resources }
The objectidentifier parameter is a reference to an object whose lock associates with the monitor that the synchronized statement represents. The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements.

What are synchronized methods and synchronized statements?

Synchronized Methods

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. 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. To make a method synchronized, simply add the synchronized keyword to its declaration:
public synchronized void increament(){ count++; }

Synchronized block

Synchronize block ensure atomicity of bunch of code statements. If you have to synchronize access to an object of a class or you only want a part of a method to be synchronized to an object then you can use synchronized block for it.
public void add(int value){ synchronized(this){ this.count += value; } }
One significant difference between synchronized method and block is that, Synchronized block generally reduce scope of lock. As scope of lock is inversely proportional to performance, its always better to lock only critical section of code. Also, Synchronized block can throw java.lang.NullPointerException if expression provided to block as parameter evaluates to null, which is not the case with synchronized methods.