Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

synchronized keyword in Java

synchronized is a keyword in Java which is used to make happens before relationship in multithreading environment to avoid memory inconsistency and thread interference error.

synchronized keyword in Java has to do with thread-safety, that is, when multiple threads read or write the same variable. This can happen directly (by accessing the same variable) or indirectly (by using a class that uses another class that accesses the same variable). The synchronized keyword is used to define a block of code where multiple threads can access the same variable in a safe way.



 synchronized use case:-1


 synchronized use case-2:-


 synchronized use case-3:-


 synchronized use case-4:-


 synchronized use case-5:-