线程间定制化调用通信—— 1 高内聚低耦合的前提下,线程操作资源类 2 判断/干活/通知 3 多线程交互中,必须要防止多线程的虚假唤醒,也即(判断只用while,不能用if)
生活随笔
收集整理的這篇文章主要介紹了
线程间定制化调用通信—— 1 高内聚低耦合的前提下,线程操作资源类 2 判断/干活/通知 3 多线程交互中,必须要防止多线程的虚假唤醒,也即(判断只用while,不能用if)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
生產者與消費者模式
一個生產者與一個消費者
題目:現在有兩個線程,可以操作初始值為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();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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java8之lambda表达式的总结
- 下一篇: 精确通知,顺序访问——Lock锁比syn