溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java并發(fā)編程中Semaphore計(jì)數(shù)信號量的示例分析

發(fā)布時(shí)間:2021-08-11 10:48:28 來源:億速云 閱讀:135 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“Java并發(fā)編程中Semaphore計(jì)數(shù)信號量的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Java并發(fā)編程中Semaphore計(jì)數(shù)信號量的示例分析”這篇文章吧。

Semaphore 是一個(gè)計(jì)數(shù)信號量,它的本質(zhì)是一個(gè)共享鎖。信號量維護(hù)了一個(gè)信號量許可集。線程可以通過調(diào)用acquire()來獲取信號量的許可;當(dāng)信號量中有可用的許可時(shí),線程能獲取該許可;否則線程必須等待,直到有可用的許可為止。 線程可以通過release()來釋放它所持有的信號量許可(用完信號量之后必須釋放,不然其他線程可能會(huì)無法獲取信號量)。

簡單示例:

package me.socketthread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreLearn { 
  //信號量總數(shù) 
  private static final int SEM_MAX = 12; 
  public static void main(String[] args) {  
    Semaphore sem = new Semaphore(SEM_MAX); 
    //創(chuàng)建線程池 
    ExecutorService threadPool = Executors.newFixedThreadPool(3); 
    //在線程池中執(zhí)行任務(wù) 
    threadPool.execute(new MyThread(sem, 7)); 
    threadPool.execute(new MyThread(sem, 4)); 
    threadPool.execute(new MyThread(sem, 2)); 
    //關(guān)閉池 
    threadPool.shutdown(); 
  } 
} 
  class MyThread extends Thread { 
    private volatile Semaphore sem;  // 信號量 
    private int count;    // 申請信號量的大小  
     
    MyThread(Semaphore sem, int count) { 
      this.sem = sem; 
      this.count = count; 
    } 
    public void run() { 
      try { 
       // 從信號量中獲取count個(gè)許可 
        sem.acquire(count); 
        Thread.sleep(2000); 
        System.out.println(Thread.currentThread().getName() + " acquire count="+count); 
      } catch (InterruptedException e) { 
        e.printStackTrace(); 
      } finally { 
        // 釋放給定數(shù)目的許可,將其返回到信號量。 
        sem.release(count); 
        System.out.println(Thread.currentThread().getName() + " release " + count + ""); 
      } 
    } 
  }

執(zhí)行結(jié)果:

pool-1-thread-2 acquire count=4
pool-1-thread-1 acquire count=7
pool-1-thread-1 release 7
pool-1-thread-2 release 4
pool-1-thread-3 acquire count=2
pool-1-thread-3 release 2

線程1和線程2會(huì)并發(fā)執(zhí)行,因?yàn)閮烧叩男盘柫亢蜎]有超過總信號量,當(dāng)前兩個(gè)線程釋放掉信號量之后線程3才能繼續(xù)執(zhí)行。

源碼分析:

1、構(gòu)造函數(shù)

在構(gòu)造函數(shù)中會(huì)初始化信號量值,這值最終是作為鎖標(biāo)志位state的值

Semaphore sem = new Semaphore(12);//簡單來說就是給鎖標(biāo)識位state賦值為12

2、Semaphore.acquire(n);簡單理解為獲取鎖資源,如果獲取不到線程阻塞

Semaphore.acquire(n);//從鎖標(biāo)識位state中獲取n個(gè)信號量,簡單來說是state = state-n 此時(shí)state大于0表示可以獲取信號量,如果小于0則將線程阻塞
public void acquire(int permits) throws InterruptedException { 
    if (permits < 0) throw new IllegalArgumentException(); 
    //獲取鎖 
    sync.acquireSharedInterruptibly(permits); 
  }

acquireSharedInterruptibly中的操作是獲取鎖資源,如果可以獲取則將state= state-permits,否則將線程阻塞

public final void acquireSharedInterruptibly(int arg) 
      throws InterruptedException { 
    if (Thread.interrupted()) 
      throw new InterruptedException(); 
    if (tryAcquireShared(arg) < 0)//tryAcquireShared中嘗試獲取鎖資源 
      doAcquireSharedInterruptibly(arg); //將線程阻塞 
  }

tryAcquireShared中的操作是嘗試獲取信號量值,簡單來說就是state=state-acquires ,如果此時(shí)小于0則返回負(fù)值,否則返回大于新值,再判斷是否將當(dāng)線程線程阻塞

protected int tryAcquireShared(int acquires) { 
      for (;;) { 
        if (hasQueuedPredecessors()) 
          return -1; 
      //獲取state值 
        int available = getState(); 
      //從state中獲取信號量 
        int remaining = available - acquires; 
        if (remaining < 0 || 
          compareAndSetState(available, remaining)) 
        //如果信號量小于0則直接返回,表示無法獲取信號量,否則將state值修改為新值 
          return remaining; 
      } 
    }

doAcquireSharedInterruptibly中的操作簡單來說是將當(dāng)前線程添加到FIFO隊(duì)列中并將當(dāng)前線程阻塞。

/會(huì)將線程添加到FIFO隊(duì)列中,并阻塞  
private void doAcquireSharedInterruptibly(int arg)  
    throws InterruptedException {  
    //將線程添加到FIFO隊(duì)列中  
    final Node node = addWaiter(Node.SHARED);  
    boolean failed = true;  
    try {  
      for (;;) {  
        final Node p = node.predecessor();  
        if (p == head) {  
          int r = tryAcquireShared(arg);  
          if (r >= 0) {  
            setHeadAndPropagate(node, r);  
            p.next = null; // help GC  
            failed = false;  
            return;  
          }  
        }  
        //parkAndCheckInterrupt完成線程的阻塞操作  
        if (shouldParkAfterFailedAcquire(p, node) &&  
          parkAndCheckInterrupt())  
          throw new InterruptedException();  
      }  
    } finally {  
      if (failed)  
        cancelAcquire(node);  
    }  
  }

3、Semaphore.release(int permits),這個(gè)函數(shù)的實(shí)現(xiàn)操作是將state = state+permits并喚起處于FIFO隊(duì)列中的阻塞線程。

public void release(int permits) { 
    if (permits < 0) throw new IllegalArgumentException(); 
  //state = state+permits,并將FIFO隊(duì)列中的阻塞線程喚起 
    sync.releaseShared(permits); 
  }

releaseShared中的操作是將state = state+permits,并將FIFO隊(duì)列中的阻塞線程喚起。

public final boolean releaseShared(int arg) { 
    //tryReleaseShared將state設(shè)置為state = state+arg 
    if (tryReleaseShared(arg)) { 
      //喚起FIFO隊(duì)列中的阻塞線程 
      doReleaseShared(); 
      return true; 
    } 
    return false; 
  }

tryReleaseShared將state設(shè)置為state = state+arg

protected final boolean tryReleaseShared(int releases) { 
      for (;;) { 
        int current = getState(); 
        int next = current + releases; 
        if (next < current) // overflow 
          throw new Error("Maximum permit count exceeded"); 
        //將state值設(shè)置為state=state+releases 
        if (compareAndSetState(current, next)) 
          return true; 
      } 
    }

doReleaseShared()喚起FIFO隊(duì)列中的阻塞線程

private void doReleaseShared() {  
  
    for (;;) {  
      Node h = head;  
      if (h != null && h != tail) {  
        int ws = h.waitStatus;  
        if (ws == Node.SIGNAL) {  
          if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))  
            continue;      // loop to recheck cases  
          //完成阻塞線程的喚起操作  
          unparkSuccessor(h);  
        }  
        else if (ws == 0 &&  
             !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))  
          continue;        // loop on failed CAS  
      }  
      if (h == head)          // loop if head changed  
        break;  
    }  
  }

總結(jié):Semaphore簡單來說設(shè)置了一個(gè)信號量池state,當(dāng)線程執(zhí)行時(shí)會(huì)從state中獲取值,如果可以獲取則線程執(zhí)行,并且在執(zhí)行后將獲取的資源返回到信號量池中,并喚起其他阻塞線程;如果信號量池中的資源無法滿足某個(gè)線程的需求則將此線程阻塞。

Semaphore源碼:

public class Semaphore implements java.io.Serializable { 
  private static final long serialVersionUID = -3222578661600680210L; 
  private final Sync sync; 
  abstract static class Sync extends AbstractQueuedSynchronizer { 
    private static final long serialVersionUID = 1192457210091910933L; 
    //設(shè)置鎖標(biāo)識位state的初始值 
    Sync(int permits) { 
      setState(permits); 
    } 
    //獲取鎖標(biāo)識位state的值,如果state值大于其需要的值則表示鎖可以獲取 
    final int getPermits() { 
      return getState(); 
    } 
    //獲取state值減去acquires后的值,如果大于等于0則表示鎖可以獲取 
    final int nonfairTryAcquireShared(int acquires) { 
      for (;;) { 
        int available = getState(); 
        int remaining = available - acquires; 
        if (remaining < 0 || 
          compareAndSetState(available, remaining)) 
          return remaining; 
      } 
    } 
    //釋放鎖 
    protected final boolean tryReleaseShared(int releases) { 
      for (;;) { 
        int current = getState(); 
        //將state值加上release值 
        int next = current + releases; 
        if (next < current) // overflow 
          throw new Error("Maximum permit count exceeded"); 
        if (compareAndSetState(current, next)) 
          return true; 
      } 
    } 
    //將state的值減去reductions 
    final void reducePermits(int reductions) { 
      for (;;) { 
        int current = getState(); 
        int next = current - reductions; 
        if (next > current) // underflow 
          throw new Error("Permit count underflow"); 
        if (compareAndSetState(current, next)) 
          return; 
      } 
    } 
    final int drainPermits() { 
      for (;;) { 
        int current = getState(); 
        if (current == 0 || compareAndSetState(current, 0)) 
          return current; 
      } 
    } 
  } 
  //非公平鎖 
  static final class NonfairSync extends Sync { 
    private static final long serialVersionUID = -2694183684443567898L; 
    NonfairSync(int permits) { 
      super(permits); 
    } 
    protected int tryAcquireShared(int acquires) { 
      return nonfairTryAcquireShared(acquires); 
    } 
  } 
  //公平鎖 
  static final class FairSync extends Sync { 
    private static final long serialVersionUID = 2014338818796000944L; 
    FairSync(int permits) { 
      super(permits); 
    } 
    protected int tryAcquireShared(int acquires) { 
      for (;;) { 
        if (hasQueuedPredecessors()) 
          return -1; 
        int available = getState(); 
        int remaining = available - acquires; 
        if (remaining < 0 || 
          compareAndSetState(available, remaining)) 
          return remaining; 
      } 
    } 
  } 
  //設(shè)置信號量 
  public Semaphore(int permits) { 
    sync = new NonfairSync(permits); 
  } 
  public Semaphore(int permits, boolean fair) { 
    sync = fair ? new FairSync(permits) : new NonfairSync(permits); 
  } 
  //獲取鎖 
  public void acquire() throws InterruptedException { 
    sync.acquireSharedInterruptibly(1); 
  } 
  public void acquireUninterruptibly() { 
    sync.acquireShared(1); 
  } 
  public boolean tryAcquire() { 
    return sync.nonfairTryAcquireShared(1) >= 0; 
  } 
  public boolean tryAcquire(long timeout, TimeUnit unit) 
    throws InterruptedException { 
    return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); 
  } 
  public void release() { 
    sync.releaseShared(1); 
  } 
  //獲取permits值鎖 
  public void acquire(int permits) throws InterruptedException { 
    if (permits < 0) throw new IllegalArgumentException(); 
    sync.acquireSharedInterruptibly(permits); 
  } 
  public void acquireUninterruptibly(int permits) { 
    if (permits < 0) throw new IllegalArgumentException(); 
    sync.acquireShared(permits); 
  } 
  public boolean tryAcquire(int permits) { 
    if (permits < 0) throw new IllegalArgumentException(); 
    return sync.nonfairTryAcquireShared(permits) >= 0; 
  } 
  public boolean tryAcquire(int permits, long timeout, TimeUnit unit) 
    throws InterruptedException { 
    if (permits < 0) throw new IllegalArgumentException(); 
    return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout)); 
  } 
  //釋放 
  public void release(int permits) { 
    if (permits < 0) throw new IllegalArgumentException(); 
    sync.releaseShared(permits); 
  } 
  public int availablePermits() { 
    return sync.getPermits(); 
  } 
  public int drainPermits() { 
    return sync.drainPermits(); 
  } 
  protected void reducePermits(int reduction) { 
    if (reduction < 0) throw new IllegalArgumentException(); 
    sync.reducePermits(reduction); 
  } 
  public boolean isFair() { 
    return sync instanceof FairSync; 
  } 
  public final boolean hasQueuedThreads() { 
    return sync.hasQueuedThreads(); 
  } 
  public final int getQueueLength() { 
    return sync.getQueueLength(); 
  } 
  protected Collection<Thread> getQueuedThreads() { 
    return sync.getQueuedThreads(); 
  } 
  public String toString() { 
    return super.toString() + "[Permits = " + sync.getPermits() + "]"; 
  } 
}

以上是“Java并發(fā)編程中Semaphore計(jì)數(shù)信號量的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI