日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java多线程之线程虚假唤醒

發布時間:2024/2/28 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java多线程之线程虚假唤醒 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java多線程之線程虛假喚醒


本文目錄提綱

  • 問題:兩個線程對一個初始值為零的變量操作,實現一個線程加一,另一個線程減一,來十次。
  • 問題:四個線程對一個初始值為零的變量操作,實現兩個線程加一,另外兩個線程減一,來十次。

  • 1. 兩個線程對一個初始值為零的變量操作,實現一個線程加一,另一個線程減一,來十次。


    代碼實現:

    class ShareData{private int number = 0;public synchronized void increment() throws InterruptedException { // 判斷if (number!=0){this.wait();} // 干活++number;System.out.println(Thread.currentThread().getName()+"\t"+number); // 通知喚醒this.notifyAll();}public synchronized void decrement() throws InterruptedException {if (number==0){this.wait();}--number;System.out.println(Thread.currentThread().getName()+"\t"+number);this.notifyAll();} }public class ThreadDemo2 {public static void main(String[] args) {ShareData sd = new ShareData();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {sd.increment();} catch (InterruptedException e) {e.printStackTrace();}} },"A").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {sd.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"B").start();} }

    編譯結果:

    多次測試,是沒有問題的。

    2. 問題:四個線程對一個初始值為零的變量操作,實現兩個線程加一,另外兩個線程減一,來十次。


    于是把上面的代碼,線程弄多兩個,運行發現出現問題。編譯結果如下:


    通過閱讀文檔,發現:

    As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

    synchronized (obj) {while (<condition does not hold>)obj.wait();... // Perform action appropriate to condition}
    即中斷和偽喚醒是可能的,并且這個方法應該在while循環中使用

    畫圖:

    將上面代碼if換成while再執行,發現沒有問題,代碼如下:

    class ShareData {private int number = 0;public synchronized void increment() throws InterruptedException { // 判斷while (number != 0) {this.wait();} // 干活++number;System.out.println(Thread.currentThread().getName() + "\t" + number); // 通知喚醒this.notifyAll();}public synchronized void decrement() throws InterruptedException {while (number == 0) {this.wait();}--number;System.out.println(Thread.currentThread().getName() + "\t" + number);this.notifyAll();} }public class ThreadDemo2 {public static void main(String[] args) {ShareData sd = new ShareData();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(200);sd.increment();} catch (InterruptedException e) {e.printStackTrace();}}}, "A").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(300); //模擬執行其他代碼sd.decrement();} catch (InterruptedException e) {e.printStackTrace();}}}, "B").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(400);sd.increment();} catch (InterruptedException e) {e.printStackTrace();}}}, "C").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(500);sd.decrement();} catch (InterruptedException e) {e.printStackTrace();}}}, "D").start();} }

    編譯結果:


    使用Lock優化

    前置知識:

    Condition factors out the Object monitor methods (wait, notify and notifyAll) into distinct objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary Lock implementations. Where a Lock replaces the use ofsynchronized methods and statements, a Condition replaces the use of the Object monitor methods.


    優化代碼:

    import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;class ShareData {private int number = 0;private Lock lock = new ReentrantLock();private Condition condition = lock.newCondition();public synchronized void increment() throws InterruptedException{lock.lock();try{while (number != 0) { // this.wait();condition.await();} // 干活++number;System.out.println(Thread.currentThread().getName() + "\t" + number); // 通知喚醒 // this.notifyAll();condition.signalAll();}catch (Exception e){e.printStackTrace();}finally {lock.unlock();}}public synchronized void decrement() throws InterruptedException{lock.lock();try{while (number == 0) { // this.wait();condition.await();} // 干活--number;System.out.println(Thread.currentThread().getName() + "\t" + number); // 通知喚醒 // this.notifyAll();condition.signalAll();}catch (Exception e){e.printStackTrace();}finally {lock.unlock();}}public class ThreadDemo2 {public static void main(String[] args) {ShareData sd = new ShareData();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(200);sd.increment();} catch (InterruptedException e) {e.printStackTrace();}}}, "A").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(300);sd.decrement();} catch (InterruptedException e) {e.printStackTrace();}}}, "B").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(400);sd.increment();} catch (InterruptedException e) {e.printStackTrace();}}}, "C").start();new Thread(() -> {for (int i = 1; i <= 10; i++) {try {Thread.sleep(500);sd.decrement();} catch (InterruptedException e) {e.printStackTrace();}}}, "D").start();} }

    總結

    以上是生活随笔為你收集整理的Java多线程之线程虚假唤醒的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。