欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java多線程的臨界資源問題解決方案

 更新時(shí)間:2020年02月11日 12:16:44   作者:戈德里克山谷  
這篇文章主要介紹了Java多線程的臨界資源問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Java多線程的臨界資源問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

臨界資源問題的原因:某一個(gè)線程在對(duì)臨界資源進(jìn)行訪問時(shí),還沒來得及完全修改臨界資源的值,臨界資源就被其他線程拿去訪問,導(dǎo)致多個(gè)線程訪問同一資源。直觀表現(xiàn)為打印結(jié)果順序混亂。

解決方法:加鎖

靜態(tài)方法中用類鎖,非靜態(tài)方法中用對(duì)象鎖。

1.同步代碼段:synchronized(){...}

2.同步方法:使用關(guān)鍵字synchronized修飾的方法

3.使用顯式同步鎖ReentrantLock

鎖池描述的即為鎖外等待的狀態(tài)

方法一:同步代碼段:synchronized(){...}

public class SourceConflict {
  public static void main(String[] args) {
    //實(shí)例化4個(gè)售票員,用4個(gè)線程模擬4個(gè)售票員
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        synchronized(" ") {
          if (TicketCenter.restCount <= 0) {
            return;
          }
          System.out.println(Thread.currentThread().getName() + "賣出一張票,剩余" + --TicketCenter.restCount + "張票");
        }
      }
    };
    
    //用4個(gè)線程模擬4個(gè)售票員
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //開啟線程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }  
}

//實(shí)現(xiàn)四名售票員共同售票,資源共享,非獨(dú)立
//Lambda表達(dá)式或匿名內(nèi)部類內(nèi)部捕獲的局部變量必須顯式的聲明為 final 或?qū)嶋H效果的的 final 類型,而捕獲實(shí)例或靜態(tài)變量是沒有限制的
class TicketCenter{
  public static int restCount = 100; 
}

方法二:同步方法,即使用關(guān)鍵字synchronized修飾的方法

public class SourceConflict2 {
  public static void main(String[] args) {
    //實(shí)例化4個(gè)售票員,用4個(gè)線程模擬4個(gè)售票員
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        sellTicket();
      }
    };
    
    //用4個(gè)線程模擬4個(gè)售票員
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //開啟線程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }
  
  private synchronized static void sellTicket() {  
    if (TicketCenter.restCount <= 0) {
      return;
    }
    System.out.println(Thread.currentThread().getName() + "賣出一張票,剩余" + --TicketCenter.restCount + "張票");
  }
}

class TicketCenter{
  public static int restCount = 100; 
}

方法三:使用顯式同步鎖ReentrantLock

import java.util.concurrent.locks.ReentrantLock;

public class SourceConflict3 {
  public static void main(String[] args) {
    //實(shí)例化4個(gè)售票員,用4個(gè)線程模擬4個(gè)售票員
    
    //顯式鎖
    ReentrantLock lock = new ReentrantLock();
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        lock.lock();
        if (TicketCenter.restCount <= 0) {
          return;
        }
        System.out.println(Thread.currentThread().getName() + "賣出一張票,剩余" + --TicketCenter.restCount + "張票");
        lock.unlock();
      }
    };
    
    //用4個(gè)線程模擬4個(gè)售票員
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //開啟線程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }  
}
class TicketCenter{
  public static int restCount = 100; 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(57)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(57)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08
  • Spring中HandlerMethod類源碼詳細(xì)解析

    Spring中HandlerMethod類源碼詳細(xì)解析

    這篇文章主要介紹了Spring中HandlerMethod類源碼詳細(xì)解析,HandlerMethod類用于封裝控制器方法信息,包含類信息、方法Method對(duì)象、參數(shù)、注解等信息,具體的接口請(qǐng)求是可以根據(jù)封裝的信息調(diào)用具體的方法來執(zhí)行業(yè)務(wù)邏輯,需要的朋友可以參考下
    2023-11-11
  • 時(shí)間中間鍵的整理

    時(shí)間中間鍵的整理

    這篇文章主要介紹了時(shí)間中間鍵的整理的相關(guān)資料,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • 深入淺析Spring Security5中默認(rèn)密碼編碼器

    深入淺析Spring Security5中默認(rèn)密碼編碼器

    這篇文章主要介紹了Spring Security5中默認(rèn)密碼編碼器,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-05-05
  • 使用jib插件為Java應(yīng)用構(gòu)建鏡像的方法

    使用jib插件為Java應(yīng)用構(gòu)建鏡像的方法

    這篇文章主要介紹了使用jib插件為Java應(yīng)用構(gòu)建鏡像,要是用戶本地沒安裝docker,可以使用jib制作出帶有鏡像的tar文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法

    Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法

    下面小編就為大家分享一篇Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 詳解Maven打包和運(yùn)行

    詳解Maven打包和運(yùn)行

    這篇文章主要介紹了Maven打包和運(yùn)行的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • java-流的使用完結(jié)與異常處理機(jī)制(詳解)

    java-流的使用完結(jié)與異常處理機(jī)制(詳解)

    下面小編就為大家?guī)硪黄猨ava-流的使用完結(jié)與異常處理機(jī)制(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • Feign遠(yuǎn)程調(diào)用丟失請(qǐng)求頭問題

    Feign遠(yuǎn)程調(diào)用丟失請(qǐng)求頭問題

    本文介紹了在服務(wù)端項(xiàng)目中如何解決資源訪問限制問題,首先介紹了問題的產(chǎn)生,然后詳細(xì)解析了源碼,最后提出了解決方案,解決方案包括同步和異步兩種,同步時(shí)直接向Spring容器注入RequestInterceptor攔截器
    2024-09-09
  • 基于Java注解(Annotation)的自定義注解入門介紹

    基于Java注解(Annotation)的自定義注解入門介紹

    要深入學(xué)習(xí)注解,我們就必須能定義自己的注解,并使用注解,在定義自己的注解之前,我們就必須要了解Java為我們提供的元注解和相關(guān)定義注解的語法
    2013-04-04

最新評(píng)論