詳解java中的互斥鎖信號(hào)量和多線程等待機(jī)制
互斥鎖和信號(hào)量都是操作系統(tǒng)中為并發(fā)編程設(shè)計(jì)基本概念,互斥鎖和信號(hào)量的概念上的不同在于,對(duì)于同一個(gè)資源,互斥鎖只有0和1 的概念,而信號(hào)量不止于此。也就是說,信號(hào)量可以使資源同時(shí)被多個(gè)線程訪問,而互斥鎖同時(shí)只能被一個(gè)線程訪問
互斥鎖在java中的實(shí)現(xiàn)就是 ReetranLock , 在訪問一個(gè)同步資源時(shí),它的對(duì)象需要通過方法 tryLock() 獲得這個(gè)鎖,如果失敗,返回 false,成功返回true。根據(jù)返回的信息來判斷是否要訪問這個(gè)被同步的資源。看下面的例子
public class ReentranLockExample {
private static int count = 0;
private static ReentrantLock reentrantLock = new ReentrantLock();
static class MyThread extends Thread{
@Override
public void run() {
super.run();
try {
while (true){
boolean result = reentrantLock.tryLock();
if (result){
System.out.println(Thread.currentThread().getName() + "get the lock success and run the syn code " + count ++);
reentrantLock.unlock();
}else{
System.out.println(Thread.currentThread().getName() + "get the lock failed and run the syn code " + count);
}
System.out.println(Thread.currentThread().getName() + "run the asyntronized code " + count);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start();
thread2.start();
}
}
信號(hào)量相當(dāng)于一個(gè)計(jì)數(shù)器,如果線程想要訪問某個(gè)資源,則先要獲得這個(gè)資源的信號(hào)量,并且信號(hào)量?jī)?nèi)部的計(jì)數(shù)器減1 ,信號(hào)量?jī)?nèi)部的計(jì)數(shù)器大于0則意味著有可以使用的資源,當(dāng)線程使用完某個(gè)資源時(shí),必須釋放這個(gè)資源的信號(hào)量。信號(hào)量的一個(gè)作用就是可以實(shí)現(xiàn)指定個(gè)線程去同事訪問某個(gè)資源。只需要在初始化 。
信號(hào)量在 Java中的實(shí)現(xiàn)是 Semaphore ,其在初始化時(shí)傳入一個(gè)整型數(shù), 用來指定同步資源最大的并發(fā)訪問量
public class SemaphoreExample {
private static Semaphore semaphore = new Semaphore(2);
private String lock = "lock";
private static int count = 0;
static class MyThread extends Thread {
@Override
public void run() {
super.run();
try {
while (true) {
semaphore.acquire();
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + "get the lock success and run the syn code " + count++);
semaphore.release();
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
MyThread thread3 = new MyThread();
thread1.start();
thread2.start();
thread3.start();
}
}
CountDownLatch 實(shí)現(xiàn)一個(gè)等待機(jī)制,在諸如 等待與會(huì)者到達(dá)后,開始會(huì)議的使用中。ConutDownLatch 在初始化中一個(gè)計(jì)數(shù)器,用來指定需要等待的個(gè)數(shù)。在并發(fā)編程中,所解決的需求就是,等待所有的線程到達(dá)某個(gè)點(diǎn)后。才開始進(jìn)行下一步,有點(diǎn)類似于開會(huì),只有當(dāng)所有的與會(huì)人員都到齊后,會(huì)議才能開始
public class CountDownLatchExample {
private static CountDownLatch mCountDownLatch = new CountDownLatch(3);
static class MyThread extends Thread {
int awaitTime;
public MyThread(int i) {
this.awaitTime = i;
}
@Override
public void run() {
super.run();
try {
while (true) {
Thread.sleep(awaitTime);
System.out.println(Thread.currentThread().getName() + "arrived " );
mCountDownLatch.countDown();
mCountDownLatch.await(); //可以指定等待時(shí)間
System.out.println(Thread.currentThread().getName() + "start meeting " );
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
MyThread thread1 = new MyThread(500);
MyThread thread2 = new MyThread(1000);
MyThread thread3 = new MyThread(2000);
thread1.start();
thread2.start();
thread3.start();
}
}
總結(jié)
以上就是本文有關(guān)Java編程中的互斥鎖,信號(hào)量和多線程等待機(jī)制實(shí)例詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。
有興趣的朋友可以了解:Java多線程賣票實(shí)例、Java多線程并發(fā)編程(互斥鎖Reentrant Lock)等。
相關(guān)文章
Java JSqlParser解析,修改和生成SQL語(yǔ)句的實(shí)用技巧
JSQLParser 是一個(gè)強(qiáng)大的開源 Java 庫(kù),用于解析 SQL 并提供語(yǔ)法樹操作功能,本文將詳細(xì)介紹如何使用 JSQLParser,并提供常見使用場(chǎng)景的代碼示例,希望對(duì)大家有所幫助2025-04-04
JAVA中調(diào)用C語(yǔ)言函數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了JAVA中調(diào)用C語(yǔ)言函數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
手把手教你寫一個(gè)SpringBoot+gRPC服務(wù)
本文將在本地環(huán)境下搭建gRPC客戶端和服務(wù)端,并成功建立通訊發(fā)送消息的方式,從而幫助大家深入了解gRPC在Spring Boot項(xiàng)目中的應(yīng)用,有需要的小伙伴可以參考下2023-12-12
java?String到底有多長(zhǎng)?String超出長(zhǎng)度該如何解決
在Java中,由于字符串常量池的存在,String常量長(zhǎng)度限制取決于String常量在常量池中的存儲(chǔ)大小,下面這篇文章主要給大家介紹了關(guān)于java?String到底有多長(zhǎng)?String超出長(zhǎng)度該如何解決的相關(guān)資料,需要的朋友可以參考下2023-01-01
MyBatis-Plus?中?typeHandler?的使用實(shí)例詳解
本文介紹了在MyBatis-Plus中如何使用typeHandler處理json格式字段和自定義typeHandler,通過使用JacksonTypeHandler,可以簡(jiǎn)單實(shí)現(xiàn)將實(shí)體類字段轉(zhuǎn)換為json格式存儲(chǔ),感興趣的朋友跟隨小編一起看看吧2024-10-10

