
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between Object level lock and Class level lock in Java
In multithreading environment, two or more threads can access the shared resources simultaneously which can lead the inconsistent behavior of the system. Java uses concept of locks to restrict concurrent access of shared resources or objects. Locks can be applied at two levels −
- Object Level Locks − It can be used when you want non-static method or non-static block of the code should be accessed by only one thread.
- Class Level locks − It can be used when we want to prevent multiple threads to enter the synchronized block in any of all available instances on runtime. It should always be used to make static data thread safe.
Sr. No. | Key | Object Level Lock | Class Level Lock |
---|---|---|---|
1 | Basic | It can be used when you want non-static method or non-static block of the code should be accessed by only one thread | It can be used when we want to prevent multiple threads to enter the synchronized block in any of all available instances on runtime |
2 | Static/Non Static | It should always be used to make non-static data thread safe. | It should always be used to make static data thread safe. |
3 | Number of Locks | Every object the class may have their own lock | Multiple objects of class may exist but there is always one class’s class object lock available |
Example of Class Level Lock
public class ClassLevelLockExample { public void classLevelLockMethod() { synchronized (ClassLevelLockExample.class) { //DO your stuff here } } }
Example of Object Level Lock
public class ObjectLevelLockExample { public void objectLevelLockMethod() { synchronized (this) { //DO your stuff here } } }
- Related Articles
- Object level lock vs Class level lock in Java?
- Difference between system level exception and Application level exception.
- Difference Between High-Level Language and Low-Level Language
- Difference Between First level cache and Second level cache in Hibernate
- What is the difference between Monitor and Lock in C#?
- Difference between sums of odd level and even level nodes of a Binary Tree in Java
- Difference between Object and Class in Java
- What is the difference between application level gateway and hardware level gateway in information security?
- Low level difference between Slice and Splice methods in Javascript
- Broken Object-Level Authorization
- Transaction lock in MongoDB?
- Check if Caps Lock and Num Lock is on or off through Console in C#
- Difference Between Object and Class in C++
- User-level threads and Kernel-level threads
- Use ReaderWriter Lock in C#
