第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 優(yōu)化讀/寫共享資源/內存位置,無需 Atomic API,例如 AtomicInteger

Java 優(yōu)化讀/寫共享資源/內存位置,無需 Atomic API,例如 AtomicInteger

嗶嗶one 2023-03-02 10:07:20
有一個共享資源,我們需要按照以下方式對其執(zhí)行讀/寫操作:當對資源進行寫入時,不應允許讀取。當讀取正在進行時,不允許寫入,但多個讀取線程應該能夠讀取。我已經編寫了如下所述的代碼,但此代碼的問題是當單個讀取線程已獲取鎖時,所有讀取都將被阻止。此外,我正在考慮使用布爾標志,例如 canReadContinue?,F在,當 read 第一次獲取鎖時,我會將此標志翻轉為 true,如果為 true,則其他線程不應嘗試獲取鎖。class SharedResource {    Lock writeLock    public Object read() {        writeLock.acquire()        doRead()    }    public void write(Object toBeWritten) {        writeLock.acquire()        doWrite(toBeWritten)        writeLock.release()    }}預期是在沒有寫入時多個線程應該能夠讀取。更新 1:公共類共享資源{private Object writeLock = new Object();private volatile boolean canReadContinue;private volatile int readCount;public void write(Object newState) throws InterruptedException {    synchronized (writeLock) {        // To make sure no read is going on        while (readCount > 0) {            wait();        }        System.out.println("Write thread has the lock.");        doWrite(newState);    }}public Object read() {    if(canReadContinue) {        incrementCount();    } else {        synchronized (writeLock) {            System.out.println("Read thread has the lock.");            canReadContinue = true;            incrementCount();        }    }    Object result = doRead();    decrementCount();    if(readCount == 0) {        // TODO - release lock and notify    }    return result;}private synchronized void incrementCount() {    readCount++;}private synchronized void decrementCount() {    readCount--;}private void doWrite(Object newState) {    // do stuff}private Object doRead() {    return "";}}現在我需要一種機制來在“// TODO - 釋放鎖并通知”行釋放鎖,任何指針如何解決這個問題?
查看完整描述

3 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

提示:

  • 你需要一個互斥鎖;例如原始對象鎖。

  • 您需要一個當前持有邏輯讀鎖的讀者數量的計數器。

  • 您需要一個標志來說明編寫器是否持有邏輯寫鎖。

  • 當且僅當您正在獲取或釋放邏輯鎖時,您才持有互斥鎖。一旦獲得它,就釋放互斥量。

  • 您將需要使用waitnotify。

實際上,您需要1實施簡化版本ReadWriteLock。


1 - ... 為了你的家庭作業(yè)。在真實世界的程序中,您應該簡單地使用現有的ReadWriteLock類。


查看完整回答
反對 回復 2023-03-02
?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

在這里回答您更新的代碼是您需要完成的一些框架:


public class SharedResource {


  private final Object signal = new Object();

  private boolean writeLocked;

  private int readerCount;


  public void write(final Object newState) throws InterruptedException {


    this.acquireWriteLock();


    try {


      // Now we know that no read and no other write is going on.

      System.out.println("Write thread has the lock.");

      this.doWrite(newState);


    } finally {

      // make sure we release the lock in any case.

      this.realeaseWriteLock();

    }


  }


  private void acquireWriteLock() throws InterruptedException {

    synchronized (this.signal) {


      // Wait until no more readers *and* no writer holds the lock.


      // To do: Insert the condition we need to wait for:


      while (/* condition here! */ ) {

        // To do: Wait for the lock-holding thread(s) to signal that they released their lock(s).

      }


      this.writeLocked = true; // Let others know that the write lock has been taken.


    }

  }


  private void realeaseWriteLock() {

    synchronized (this.signal) {


      this.writeLocked = false;


      // To do: Notify any and all other waiting threads that we released the lock!


    }

  }


  public Object read() {


    // To be done...


  }


  private void acquireReadLock() throws InterruptedException {

    synchronized (this.signal) {


      // Wait until no *writer* holds the lock.

      // To do: Insert condition we need to wait for:


      while (/* condition here! */ ) {

         // To do: Wait for the lock-holding thread(s) to signal that they released their lock(s).


      }


      // Now we know that no writer holds the lock. Acquire (another) read lock:


      this.readerCount++;


    }

  }


  private void releaseReadLock() throws InterruptedException {

    synchronized (this.signal) {


      this.readerCount--;


      // To do: Notify any threads waiting (i.e. writer threads).


      // (In fact only *required* if there are *no* more readers now because that's the only condition any thread will wait on.)


    }

  }


  private void doWrite(final Object newState) {

    // do stuff

  }


  private Object doRead() {

    return "";

  }


}

要理解的要點可能是每次嘗試獲取鎖都可能必須wait,并且每次釋放鎖都應該有notify任何(潛在的)等待線程。


查看完整回答
反對 回復 2023-03-02
?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

此外,我正在考慮使用布爾標志,例如 canReadContinue

你在正確的軌道上。但是請記住,任何數量的線程都可以同時執(zhí)行它們的讀取訪問,并且只有在當前沒有其他線程正在讀取或寫入的情況下才能進行寫入訪問。

所以你需要跟蹤當前有多少讀者持有鎖,并且每個讀者都必須確保在完成后釋放鎖。只有當 0 個讀者(和 0 個寫者)持有鎖時,一個寫者才可以繼續(xù);并且僅當 & 當 0 個寫入者持有鎖時,任何讀取者都可以繼續(xù)。


查看完整回答
反對 回復 2023-03-02
  • 3 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號