日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

线程间定制化调用通信—— 1 高内聚低耦合的前提下,线程操作资源类 2 判断/干活/通知 3 多线程交互中,必须要防止多线程的虚假唤醒,也即(判断只用while,不能用if)

發布時間:2025/4/16 65 豆豆

生產者與消費者模式



一個生產者與一個消費者


題目:現在有兩個線程,可以操作初始值為0的一個變量,實現一個線程對該變量加1,另一個線程對該變量減1,這兩個線程的操作加一.減一交替,進行10輪,變量的初始值為0
???? 高內聚低耦合的前提下,線程操作資源類

package com.dym.juc;class AirConditioner{private int number=0;public synchronized void increment() throws InterruptedException {//1.判斷if(number!=0){this.wait();}//2.干活number++;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();}public synchronized void decrement() throws InterruptedException {//1.判斷if(number==0){this.wait();}//2.干活number--;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();} }// //題目:現在有兩個線程,可以操作初始值為0的一個變量 // 實現一個線程對該變量加1,另一個線程對該變量減1 // 這兩個線程的操作加一,減一交替,進行10輪,變量的初始值為0 // 高內聚低耦合的前提下,線程操作資源類 // 判斷/干活/通知public class ThreadWaitNotifyDemo {public static void main(String[] args) {AirConditioner airConditioner=new AirConditioner();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {airConditioner.increment();} catch (InterruptedException e) {e.printStackTrace();}}},"A").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {airConditioner.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"B").start();} }



兩個生產者與兩個消費者

package com.dym.juc;class AirConditioner{private int number=0;public synchronized void increment() throws InterruptedException {//1.判斷if(number!=0){this.wait();}//2.干活number++;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();}public synchronized void decrement() throws InterruptedException {//1.判斷if(number==0){this.wait();}//2.干活number--;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();} }// //題目:現在有兩個線程,可以操作初始值為0的一個變量 // 實現一個線程對該變量加1,另一個線程對該變量減1 // 這兩個線程的操作加一,減一交替,進行10輪,變量的初始值為0 // 高內聚低耦合的前提下,線程操作資源類 // 判斷/干活/通知public class ThreadWaitNotifyDemo {public static void main(String[] args) {AirConditioner airConditioner=new AirConditioner();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {airConditioner.increment();} catch (InterruptedException e) {e.printStackTrace();}}},"A").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {airConditioner.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"B").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {airConditioner.increment();} catch (InterruptedException e) {e.printStackTrace();}}},"C").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {airConditioner.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"D").start();} }



package com.dym.juc;class AirConditioner{private int number=0;public synchronized void increment() throws InterruptedException {//1.判斷if(number!=0){this.wait();}//2.干活number++;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();}public synchronized void decrement() throws InterruptedException {//1.判斷if(number==0){this.wait();}//2.干活number--;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();} }// //題目:現在有兩個線程,可以操作初始值為0的一個變量 // 實現一個線程對該變量加1,另一個線程對該變量減1 // 這兩個線程的操作加一,減一交替,進行10輪,變量的初始值為0 // 高內聚低耦合的前提下,線程操作資源類 // 判斷/干活/通知public class ThreadWaitNotifyDemo {public static void main(String[] args) {AirConditioner airConditioner=new AirConditioner();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(200);airConditioner.increment();} catch (InterruptedException e) {e.printStackTrace();}}},"A").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(300);airConditioner.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"B").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(400);airConditioner.increment();} catch (InterruptedException e) {e.printStackTrace();}}},"C").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(500);airConditioner.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"D").start();} }



多線程交互中,必須要防止多線程的虛假喚醒,也即(判斷只用while,不能用if)

package com.dym.juc;class AirConditioner{private int number=0;public synchronized void increment() throws InterruptedException {//1.判斷while (number!=0){this.wait();}//2.干活number++;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();}public synchronized void decrement() throws InterruptedException {//1.判斷while (number==0){this.wait();}//2.干活number--;System.out.println(Thread.currentThread().getName()+"\t"+number);//3.通知this.notifyAll();} }// //題目:現在有兩個線程,可以操作初始值為0的一個變量 // 實現一個線程對該變量加1,另一個線程對該變量減1 // 這兩個線程的操作加一,減一交替,進行10輪,變量的初始值為0 // 1 高內聚低耦合的前提下,線程操作資源類 // 2 判斷/干活/通知 // 3 多線程交互中,必須要防止多線程的虛假喚醒,也即(判斷只用while,不能用if)public class ThreadWaitNotifyDemo {public static void main(String[] args) {AirConditioner airConditioner=new AirConditioner();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(200);airConditioner.increment();} catch (InterruptedException e) {e.printStackTrace();}}},"A").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(300);airConditioner.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"B").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(400);airConditioner.increment();} catch (InterruptedException e) {e.printStackTrace();}}},"C").start();new Thread(()->{for (int i = 1; i <=10 ; i++) {try {Thread.sleep(500);airConditioner.decrement();} catch (InterruptedException e) {e.printStackTrace();}}},"D").start();} }



總結

以上是生活随笔為你收集整理的线程间定制化调用通信—— 1 高内聚低耦合的前提下,线程操作资源类 2 判断/干活/通知 3 多线程交互中,必须要防止多线程的虚假唤醒,也即(判断只用while,不能用if)的全部內容,希望文章能夠幫你解決所遇到的問題。

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