如何解決Java多線程的臨界資源問題

小編給大家分享一下如何解決Java多線程的臨界資源問題,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:空間域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、尉氏網(wǎng)站維護、網(wǎng)站推廣。

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

解決方法:加鎖

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

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

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

3.使用顯式同步鎖ReentrantLock

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

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

public class SourceConflict {
  public static void main(String[] args) {
    //實例化4個售票員,用4個線程模擬4個售票員
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        synchronized(" ") {
          if (TicketCenter.restCount <= 0) {
            return;
          }
          System.out.println(Thread.currentThread().getName() + "賣出一張票,剩余" + --TicketCenter.restCount + "張票");
        }
      }
    };
    
    //用4個線程模擬4個售票員
    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();
    
  }  
}

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

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

public class SourceConflict2 {
  public static void main(String[] args) {
    //實例化4個售票員,用4個線程模擬4個售票員
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        sellTicket();
      }
    };
    
    //用4個線程模擬4個售票員
    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) {
    //實例化4個售票員,用4個線程模擬4個售票員
    
    //顯式鎖
    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個線程模擬4個售票員
    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; 
}

看完了這篇文章,相信你對“如何解決Java多線程的臨界資源問題”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

新聞名稱:如何解決Java多線程的臨界資源問題
文章來源:http://www.muchs.cn/article32/gceppc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名微信小程序、搜索引擎優(yōu)化網(wǎng)站設(shè)計、域名注冊營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)