最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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; 
}

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

相關(guān)文章

  • Java8需要知道的4個(gè)函數(shù)式接口簡單教程

    Java8需要知道的4個(gè)函數(shù)式接口簡單教程

    這篇文章主要介紹了Java?8中引入的函數(shù)式接口,包括Consumer、Supplier、Predicate和Function,以及它們的用法和特點(diǎn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • SpringAop @Around執(zhí)行兩次的原因及解決

    SpringAop @Around執(zhí)行兩次的原因及解決

    這篇文章主要介紹了SpringAop @Around執(zhí)行兩次的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java8中的List<String>轉(zhuǎn)List<Integer>的實(shí)例代碼

    java8中的List<String>轉(zhuǎn)List<Integer>的實(shí)例代碼

    這篇文章主要介紹了java8中的List<String>轉(zhuǎn)List<Integer>,轉(zhuǎn)換list列表String到列表Intger,java8提供了stream很好的進(jìn)行操作,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • 詳解SpringBoot整合MyBatis詳細(xì)教程

    詳解SpringBoot整合MyBatis詳細(xì)教程

    這篇文章主要介紹了詳解SpringBoot整合MyBatis詳細(xì)教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • IDEA 2021.1 的 Win 和 Mac 快捷鍵大全

    IDEA 2021.1 的 Win 和 Mac 快捷鍵大全

    這篇文章主要介紹了IDEA 2021.1 的 Win 和 Mac 快捷鍵大全,本文是小編給大家精心收藏的,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • mybatis參數(shù)類型不匹配錯(cuò)誤argument type mismatch的處理方案

    mybatis參數(shù)類型不匹配錯(cuò)誤argument type mismatch的處理方案

    這篇文章主要介紹了mybatis參數(shù)類型不匹配錯(cuò)誤argument type mismatch的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java函數(shù)式編程(十一):遍歷目錄

    Java函數(shù)式編程(十一):遍歷目錄

    這篇文章主要介紹了Java函數(shù)式編程(十一):遍歷目錄,本文是系列文章的第11篇,其它文章請(qǐng)參閱本文底部的相關(guān)文章,需要的朋友可以參考下
    2014-09-09
  • mybatis批量插入時(shí),有字段可能為null會(huì)報(bào)錯(cuò)問題

    mybatis批量插入時(shí),有字段可能為null會(huì)報(bào)錯(cuò)問題

    這篇文章主要介紹了mybatis批量插入時(shí),有字段可能為null會(huì)報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Jenkins Pipeline 部署 SpringBoot 應(yīng)用的教程詳解

    Jenkins Pipeline 部署 SpringBoot 應(yīng)用的教程詳解

    這篇文章主要介紹了Jenkins Pipeline 部署 SpringBoot 應(yīng)用的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 基于Java實(shí)現(xiàn)掃碼登錄的示例代碼

    基于Java實(shí)現(xiàn)掃碼登錄的示例代碼

    相信大家對(duì)二維碼都不陌生,生活中到處充斥著掃碼登錄的場景,如登錄網(wǎng)頁版微信、支付寶等。本文將利用Java實(shí)現(xiàn)一個(gè)簡易版掃碼登錄的 Demo,需要的可以參考一下
    2022-04-04

最新評(píng)論

花莲市| 平原县| 维西| 陈巴尔虎旗| 阳泉市| 分宜县| 乌什县| 台湾省| 西乌| 靖西县| 临夏县| 资阳市| 凤冈县| 临邑县| 通榆县| 剑川县| 垦利县| 九龙城区| 布拖县| 南城县| 红桥区| 揭东县| 上高县| 同仁县| 芜湖县| 手机| 紫金县| 琼中| 鲁山县| 泸水县| 永康市| 富川| 简阳市| 藁城市| 茶陵县| 靖边县| 翼城县| 广德县| 堆龙德庆县| 旬邑县| 澜沧|