生产者与消费者模式
一个生产者与一个消费者
题目:现在有两个线程,可以操作初始值为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();
}
}