<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 互斥量示例 > 原文: [https://javatutorial.net/java-mutex-example](https://javatutorial.net/java-mutex-example) 在更深入地了解互斥量之前,先給出一個示例: 想想一個隊列。 不管長短,都沒關系。 現在想想一輛正在出售游樂園門票的卡車。 一次一個人可以買票。 該人買了票后,就該排隊了。 這個小故事與理解互斥量有什么關系? 讓我解釋。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) **互斥量允許每個線程具有 1 個許可**。換句話說,一次只能有 1 個線程可以訪問資源。 在上面的類比中,兩個人不能同時購買門票。互斥量也是如此。 它不是線程,而是人員,而是票證。 同樣的事情或多或少.. 互斥量與`Semaphore`略有不同,因此`Semaphore`允許多個線程訪問資源。意思是,多個人可以同時購買門票。 ![Mutex java example thread](https://img.kancloud.cn/b3/f7/b3f7cff10469ea04e019847bcc26a167_964x337.jpg) ## 構造器 1. `public Semaphore(int permits)`; 2. `public Semaphore(int permits, boolean fair)` 第一個構造函數是我們實際上可以區分互斥量和`Semaphore`的地方。 如果那里有 1 作為參數,則意味著將只允許 1 個線程獲取鎖。 請記住,由于它沒有第二個參數`boolean fair`,因此您正在使`Semaphore`類以任何順序訪問任何線程。 第二個構造函數如果傳遞`true`(公平),則確保按線程請求訪問并在隊列中等待的順序給出訪問。 ## 互斥量基本代碼實現 ```java import java.util.concurrent.Semaphore; public class MutexDemo { // create a Semaphore instance that makes it so only 1 thread can access resource at a time private static Semaphore mutex = new Semaphore(1); static class ThreadDemo extends Thread { private String name = ""; public ThreadDemo(String name) { this.name = name; } @Override public void run() { try { //?check?the?above?mentioned?analogy?in?the?article?for?reference System.out.println("How many people can buy a ticket at a time: " + mutex.availablePermits()); System.out.println(name + " is buying a ticket..."); mutex.acquire(); try { Thread.sleep(1000); System.out.println(name + " is still buying a ticket. How many people can still buy the ticket alongside him: " + mutex.availablePermits()); } finally { mutex.release(); System.out.println(name + " bought the ticket."); System.out.println("How many people can buy tickets after " + name + " has finished buying the ticket: " + mutex.availablePermits()); } } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { ThreadDemo thread1 = new ThreadDemo("Bob"); thread1.start(); ThreadDemo thread2 = new ThreadDemo("Charlie"); thread2.start(); ThreadDemo thread3 = new ThreadDemo("Christie"); thread3.start(); } } ``` **輸出** ```java How many people can buy a ticket at a time: 1 Bob is buying a ticket... How many people can buy a ticket at a time: 0 Charlie is buying a ticket... How many people can buy a ticket at a time: 0 Christie is buying a ticket... Bob is still buying a ticket. How many people can still buy the ticket alongside him: 0 Bob bought the ticket. How many people can buy tickets after Bob has finished buying the ticket: 1 Charlie is still buying a ticket. How many people can still buy the ticket alongside him: 0 Charlie bought the ticket. How many people can buy tickets after Charlie has finished buying the ticket: 1 Christie is still buying a ticket. How many people can still buy the ticket alongside him: 0 Christie bought the ticket. How many people can buy tickets after Christie has finished buying the ticket: 1 ``` 從輸出中可以看到,**當有人買票時,沒有其他人可以買**。這是顯示以下內容的行之一: Bob 仍在買票。 還有多少人可以和他一起買票: **0** 但是,在他“購買”了機票之后,其他人立即購買了機票。 歸根結底,它涉及`acquire()`和`release()`。`acquire()`是指該人開始“購買機票”的時間,`release()`是指該人“購買機票”的時間。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看