SoFunction
Updated on 2025-03-03

Four ways to add multithreaded locks in Java

In Java, there are mainly the following ways to add multi-threaded locks:

1. Use synchronized keywords

synchronizedCan be used in a method or code block to ensure that only one thread can access the locked code.

Method lock

class SynchronizedMethod {
    public synchronized void synchronizedMethod() {
        ("Synchronized method executed by " + ().getName());
    }
}

Code block lock

class SynchronizedBlock {
    private final Object lock = new Object();

    public void synchronizedBlock() {
        synchronized (lock) {
            ("Synchronized block executed by " + ().getName());
        }
    }
}

2. Use ReentrantLock class

ReentrantLockyesA lock in the package withsynchronizedMore flexible locking mechanism.

import ;

class ReentrantLockExample {
    private final ReentrantLock lock = new ReentrantLock();

    public void lockMethod() {
        ();
        try {
            ("ReentrantLock method executed by " + ().getName());
        } finally {
            ();
        }
    }
}

3. Use ReadWriteLock

ReadWriteLockAllows multiple read threads to access shared resources at the same time, but prevents read and write access from other threads when they are accessed by the write thread.

import ;
import ;

class ReadWriteLockExample {
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public void readMethod() {
        ().lock();
        try {
            ("Read lock method executed by " + ().getName());
        } finally {
            ().unlock();
        }
    }

    public void writeMethod() {
        ().lock();
        try {
            ("Write lock method executed by " + ().getName());
        } finally {
            ().unlock();
        }
    }
}

4. Use Semaphore

SemaphoreIt is a counting semaphore that can limit the number of threads accessing a resource at the same time.

import ;

class SemaphoreExample {
    private final Semaphore semaphore = new Semaphore(2); // Allow up to two threads to access
    public void accessResource() {
        try {
            ();
            ("Accessing resource by " + ().getName());
            (1000); // Simulation work        } catch (InterruptedException e) {
            ();
        } finally {
            ();
        }
    }
}

Summarize

  • synchronizedIt is a built-in keyword in Java, easy to use, but lacks flexibility.
  • ReentrantLockProvides more powerful lock control capabilities, such as reentry locks and timeout locks.
  • ReadWriteLockAllows more efficient processing of read and write operations, suitable for scenarios where more reads and fewer writes.
  • SemaphoreIt can control the number of threads accessing resources, suitable for scenarios with limited resources.

These locking mechanisms can help you achieve thread-safe access in a multi-threaded environment

This is the end of this article about four ways to add multi-threaded locks in Java. For more related Java multi-threaded locks, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!