What is synchronized in Java?

What is Synchronized in Java?

Synchronized is a Java keyword that enables mutual exclusion and semaphore access to shared resources. It’s a fundamental concept in Java concurrency, allowing developers to write thread-safe code that prevents data corruption and ensures thread safety.

What is Mutual Exclusion?

Mutual exclusion is a synchronization technique that ensures only one thread can access a shared resource at a time. This is achieved by acquiring a lock on the resource, which prevents other threads from accessing it until the lock is released.

What is Semaphore?

A semaphore is a variable that controls the access to a shared resource by multiple threads. It’s essentially a counter that limits the number of threads that can access the resource simultaneously. When a thread acquires the semaphore, it can access the resource, and when it releases the semaphore, other threads can access the resource.

Why is Synchronized Necessary?

Synchronized is necessary in Java because it prevents data corruption and ensures thread safety. When multiple threads access shared resources simultaneously, it’s easy to introduce errors, such as:

  • Data inconsistency: Multiple threads may modify the shared resource simultaneously, leading to inconsistent data.
  • Data corruption: A thread may overwrite data written by another thread, resulting in corrupted data.
  • Deadlocks: Two or more threads are blocked indefinitely, waiting for each other to release resources.

How Synchronized Works

Here’s a step-by-step explanation of how synchronized works:

  1. Thread creation: A new thread is created, and it acquires the lock on the shared resource.
  2. Lock acquisition: The thread acquires the lock on the shared resource, ensuring exclusive access.
  3. Resource access: The thread can now access the shared resource.
  4. Lock release: The thread releases the lock on the shared resource, allowing other threads to access the resource.
  5. Thread completion: The thread completes its execution, and the lock is released.

Benefits of Synchronized

Synchronized provides several benefits, including:

  • Thread safety: Synchronized ensures that multiple threads access shared resources safely and reliably.
  • Improved performance: By avoiding data corruption and deadlocks, synchronized code can run faster and more efficiently.
  • Easier debugging: Synchronized code is easier to debug, as it provides a clear understanding of the synchronization mechanism.

Common Synchronized Methods

Here are some common synchronized methods in Java:

  • synchronized (this): Acquires the lock on the current object.
  • synchronized (Object): Acquires the lock on the object itself.
  • synchronized (Object, this): Acquires the lock on the object and the current object.
  • synchronized (this, Object): Acquires the lock on the current object and the object itself.

Table: Synchronized Methods

Method Description
synchronized (this) Acquires the lock on the current object.
synchronized (Object) Acquires the lock on the object itself.
synchronized (Object, this) Acquires the lock on the object and the current object.
synchronized (this, Object) Acquires the lock on the current object and the object itself.

Example Code

Here’s an example code that demonstrates the use of synchronized:

public class SynchronizedExample {
private static Object lock = new Object();

public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
synchronized (lock) {
System.out.println("Thread 1: Accessing shared resource");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Thread 1: Finished accessing shared resource");
}
});

Thread thread2 = new Thread(() -> {
synchronized (lock) {
System.out.println("Thread 2: Accessing shared resource");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Thread 2: Finished accessing shared resource");
}
});

thread1.start();
thread2.start();
}
}

In this example, two threads access a shared resource using synchronized. The output will be:

Thread 1: Accessing shared resource
Thread 2: Accessing shared resource
Thread 1: Finished accessing shared resource
Thread 2: Finished accessing shared resource

Conclusion

Synchronized is a fundamental concept in Java concurrency that ensures thread safety and prevents data corruption. By understanding the basics of synchronized, developers can write efficient and reliable code that meets the needs of their applications. In this article, we’ve covered the basics of synchronized, including what it is, why it’s necessary, how it works, and common synchronized methods. We’ve also provided example code to demonstrate the use of synchronized.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top