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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 線程同步 > 原文: [https://javatutorial.net/java-thread-synchronization](https://javatutorial.net/java-thread-synchronization) 本文討論了 Java 中線程同步的重要性以及如何在程序中實現線程同步。 當您的程序有多個線程時,當不同的線程嘗試訪問相同的資源或同時執行相同的操作時,可能會出現問題。 這種情況會導致錯誤和并發問題。 讓我們以一個示例為例,您的程序中有兩個線程試圖讀取/寫入文本文件。 當兩個線程都在寫入文件時,一個線程的數據可能會被另一個線程覆蓋,并且在讀取文件時會發現相同的問題。 因此,線程同??步是必需的,以便多個線程在任何給定時間點一次訪問一個線程。 為什么要使用線程同步? * 防止線程干擾 * 避免一致性問題和并發問題 * 防止數據丟失 線程同步有互斥和線程間通信兩種類型。 * 互斥 * 同步方法。 * 同步塊。 * 靜態同步。 * 合作(Java 中的線程間通信) ## 同步塊 同步塊用于線程同步。 `synchronized`關鍵字用于標識 Java 中的同步塊,并基于某些對象進行同步。 其背后的主要概念是,在同一對象上同步的所有同步塊一次只能在其中一個線程內執行,這會阻止多個線程同時運行和執行。 嘗試進入同步塊的所有其他線程將被阻止,直到同步塊內的線程退出該塊為止。 共享資源保留在此同步塊內,以便在給定時間點只有一個線程可以訪問特定資源。 同步塊的語法如下所示: ```java synchronized(referencetoobject) { // Shared variables and other shared resources are placed here } ``` ## 監視器 在嘗試理解和實現 Java 同步時,監視器的概念很重要。 * Java 中的每個對象都與一個監視器關聯 * 線程可以鎖定或解鎖監視器 * 在給定時間,只有一個線程可以擁有監視器。 * 一次只能有一個線程可以鎖定監視器 ## 線程同步示例 ```java //We write a program with a simple counter execution. class Countings { public void printing() { try { for(int i = 5; i > 0; i--) { System.out.println( i ); } } catch (Exception e) { System.out.println("Thread interrupted."); } } } class Threadings extends Thread { private Thread thrd; private String thrdName; Countings gg; Threadings( String name, Countings abc) { thrdName = name; gg = abc; } public void run() { gg.printing(); System.out.println("Thread " + thrdName + " exiting."); } public void start () { System.out.println("Starting " + thrdName ); if (thrd == null) { thrd = new Thread (this, thrdName); thrd.start (); } } } public class Tests { public static void main(String args[]) { Countings gg = new Countings(); Countings T1 = new Countings ( "Thread - 1 ", gg ); Countings T2 = new Countings ( "Thread - 2 ", gg ); T1.start(); T2.start(); // threads take some time to end try { T1.join(); T2.join(); } catch ( Exception e) { System.out.println("Interrupted"); } } } ``` 輸出:(每次運行都會產生不同的結果) ```java Starting Thread - 1 Starting Thread - 2 5 4 3 5 2 1 4 Thread Thread - 1? exiting. 3 2 1 Thread Thread - 2? exiting. ``` 相同的示例,但是這次具有線程同步: ```java class Countings { public void printings() { try { for(int i = 5; i > 0; i--) { System.out.println( i ); } } catch (Exception e) { System.out.println("Thread interrupted."); } } } class Threadings extends Thread { private Thread t; private String thrdName; Countings gg; Threadings( String name, Countings abc) { thrdName = name; gg = abc; } public void run() { synchronized(gg) { gg.printings(); } System.out.println("Thread " + threadName + " exiting."); } public void start () { System.out.println("Starting " + thrdName ); if (thrd == null) { thrd = new Thread (this, thrdName); thrd.start (); } } } public class Testings{ public static void main(String args[]) { Countings gg = new Countings(); Threadings T1 = new Threadings ( "Thread - 1 ", gg ); Threadings T2 = new Threadings ( "Thread - 2 ", gg ); T1.start(); T2.start(); // wait for threads to end try { T1.join(); T2.join(); } catch ( Exception e) { System.out.println("Interrupted"); } } } ``` 輸出:(我們看到的輸出是同步的,并且每次執行程序都是相同的) ```java Starting Thread - 1 Starting Thread - 2 5 4 3 2 1 Thread Thread - 1? exiting. 5 4 3 2 1 Thread Thread - 2? exiting. ```
                  <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>

                              哎呀哎呀视频在线观看