java中多線程加鎖的四種方式
在Java中,多線程加鎖的方式主要有以下幾種:
1. 使用synchronized關(guān)鍵字
synchronized可以用于方法或代碼塊,確保只有一個(gè)線程能訪問被鎖定的代碼。
方法鎖
class SynchronizedMethod {
public synchronized void synchronizedMethod() {
System.out.println("Synchronized method executed by " + Thread.currentThread().getName());
}
}
代碼塊鎖
class SynchronizedBlock {
private final Object lock = new Object();
public void synchronizedBlock() {
synchronized (lock) {
System.out.println("Synchronized block executed by " + Thread.currentThread().getName());
}
}
}
2. 使用ReentrantLock類
ReentrantLock是java.util.concurrent.locks包中的一種鎖,具有比synchronized更靈活的鎖定機(jī)制。
import java.util.concurrent.locks.ReentrantLock;
class ReentrantLockExample {
private final ReentrantLock lock = new ReentrantLock();
public void lockMethod() {
lock.lock();
try {
System.out.println("ReentrantLock method executed by " + Thread.currentThread().getName());
} finally {
lock.unlock();
}
}
}
3. 使用讀寫鎖(ReadWriteLock)
ReadWriteLock允許多個(gè)讀線程同時(shí)訪問共享資源,但在寫線程訪問時(shí),阻止其他線程的讀和寫訪問。
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class ReadWriteLockExample {
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public void readMethod() {
readWriteLock.readLock().lock();
try {
System.out.println("Read lock method executed by " + Thread.currentThread().getName());
} finally {
readWriteLock.readLock().unlock();
}
}
public void writeMethod() {
readWriteLock.writeLock().lock();
try {
System.out.println("Write lock method executed by " + Thread.currentThread().getName());
} finally {
readWriteLock.writeLock().unlock();
}
}
}
4. 使用信號(hào)量(Semaphore)
Semaphore是一種計(jì)數(shù)信號(hào)量,可以限制同時(shí)訪問某個(gè)資源的線程數(shù)量。
import java.util.concurrent.Semaphore;
class SemaphoreExample {
private final Semaphore semaphore = new Semaphore(2); // 允許最多兩個(gè)線程訪問
public void accessResource() {
try {
semaphore.acquire();
System.out.println("Accessing resource by " + Thread.currentThread().getName());
Thread.sleep(1000); // 模擬工作
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
}
}
總結(jié)
synchronized是 Java 內(nèi)置的關(guān)鍵字,易于使用,但靈活性不足。ReentrantLock提供了更強(qiáng)大的鎖控制能力,如可重入鎖、超時(shí)鎖。ReadWriteLock允許更高效地處理讀寫操作,適用于讀多寫少的場景。Semaphore可以控制訪問資源的線程數(shù)量,適合有限資源的場景。
這些加鎖機(jī)制可以幫助你在多線程環(huán)境中實(shí)現(xiàn)線程安全的訪問
到此這篇關(guān)于java中多線程加鎖的四種方式的文章就介紹到這了,更多相關(guān)java 多線程加鎖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity HttpSecurity 類處理流程分析
SpringSecurity在SSM項(xiàng)目中使用基于配置文件,通過XML標(biāo)簽定義認(rèn)證信息,HttpSecurity在SpringBoot中通過代碼配置實(shí)現(xiàn)與XML相同功能,詳細(xì)介紹了HttpSecurity的類結(jié)構(gòu)、處理過程及其與SecurityBuilder的關(guān)系,感興趣的朋友一起看看吧2024-09-09
Java中如何將JSON格式的字符串轉(zhuǎn)換為Map
在 Java 中,將 JSON 格式的字符串轉(zhuǎn)換為?Map可以借助一些流行的 JSON 處理庫來實(shí)現(xiàn),下面為你介紹使用 Gson 和 Jackson 這兩個(gè)常用庫的方法,感興趣的朋友一起看看吧2025-04-04
SpringBoot原生組件注入實(shí)現(xiàn)兩種方式介紹
SpringBoot是Spring全家桶的成員之一,基于約定優(yōu)于配置的思想(即有約定默認(rèn)值,在不配置的情況下會(huì)使用默認(rèn)值,在配置文件下配置的話會(huì)使用配置的值)。SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時(shí)也是簡化Spring的一種快速開發(fā)的腳手架2022-10-10
mybatis?plus框架@TableField注解不生效問題及解決方案
最近遇到一個(gè)mybatis plus的問題,@TableField注解不生效,導(dǎo)致查出來的字段反序列化后為空,今天通過本文給大家介紹下mybatis?plus框架的@TableField注解不生效問題總結(jié),需要的朋友可以參考下2022-03-03
spring boot 加載web容器tomcat流程源碼分析
本文章主要描述spring boot加載web容器 tomcat的部分,為了避免文章知識(shí)點(diǎn)過于分散,其他相關(guān)的如bean的加載,tomcat內(nèi)部流程等不做深入討論,具體內(nèi)容詳情跟隨小編一起看看吧2021-06-06
Spring Security自定義身份認(rèn)證的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Security自定義身份認(rèn)證的實(shí)現(xiàn)方法,下面對(duì)Spring Security的這三種自定義身份認(rèn)證進(jìn)行詳細(xì)講解,需要的朋友可以參考下2025-04-04
java數(shù)據(jù)類型和運(yùn)算符的深入講解
這篇文章主要給大家介紹了關(guān)于java數(shù)據(jù)類型和運(yùn)算符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

