Java经典多线程问题--生产者与消费者

今天研究了一下Java多线程,顺便写了一下多线程中的经典问题—–生产者消费者经典问题,推荐一个线程链接Java基础知识回顾–线程
这个里面其实写了生产者与消费者问题,估计在后面大家看起来比较费劲,所以我提取出来再讲解一遍。

package yanning;

public class ProducerConsumer {

    public static void main(String[] args) {
        Tong tong = new Tong();
        Producer p = new Producer(tong);
        Consumer c = new Consumer(tong);
        new Thread(p).start();
        new Thread(c).start();
    }
}
class Baozi {
    int id;
    Baozi(int id) {
        this.id = id;
    }
    public String toString() {
        return "Baozi:" + id;
    }
}
class Tong {
    int index;
    Baozi[] BZ = new Baozi[10];

    public synchronized void push(Baozi baozi) {
        if(index == BZ.length) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        BZ[index] = baozi;
        index ++;
    }
    public synchronized Baozi pop() {
        if(index == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        this.notify();
        index --;
        return BZ[index];
    }
}

class Producer implements Runnable{
    Tong tong = null;
    Producer(Tong tong) {
        this.tong = tong;
    }

    public void run() {
        for( int i = 0; i < 10; i ++ ) {
            Baozi baozi = new Baozi( i );
            tong.push(baozi);
        System.out.println("生产了 :" + baozi);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
    }
}

class Consumer implements Runnable{
    Tong tong = null;
    Consumer(Tong tong) {
        this.tong = tong;
    }

    public void run() {
        for( int i = 0; i < 10; i ++ ) {
            Baozi baozi = tong.pop();
            System.out.println("消费了:" + baozi);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }   
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生产者消费者问题是一种经典多线程同步问题,主要涉及到生产者消费者之间的数据交换和同步。 在Java中,可以使用多种方式来实现生产者消费者问题的解决方案,其中最常用的方式是使用wait()和notify()方法来实现线程间的同步。 下面是一个简单的示例,演示了如何使用wait()和notify()方法来实现生产者消费者问题的解决方案: ``` import java.util.LinkedList; import java.util.Queue; public class ProducerConsumer { private Queue<Integer> buffer = new LinkedList<>(); private final int capacity = 5; public void produce() throws InterruptedException { int value = 0; while (true) { synchronized (this) { while (buffer.size() == capacity) wait(); System.out.println("Producer produced-" + value); buffer.add(value++); notify(); Thread.sleep(1000); } } } public void consume() throws InterruptedException { while (true) { synchronized (this) { while (buffer.size() == 0) wait(); int val = buffer.poll(); System.out.println("Consumer consumed-" + val); notify(); Thread.sleep(1000); } } } } ``` 在上面的示例中,ProducerConsumer类表示生产者消费者问题的解决方案。这个类有一个buffer队列,它是一个FIFO队列,用于存储生产者生产的数据。 produce()方法表示生产者的行为,它使用一个while(true)循环来不断地生产数据。在每次生产之前,它首先检查buffer队列是否已满。如果buffer队列已满,则调用wait()方法来等待消费者线程消费数据。如果buffer队列未满,则将生产的数据添加到buffer队列中,并调用notify()方法通知消费者线程可以消费数据了。 consume()方法表示消费者的行为,它使用一个while(true)循环来不断地消费数据。在每次消费之前,它首先检查buffer队列是否为空。如果buffer队列为空,则调用wait()方法来等待生产者线程生产数据。如果buffer队列不为空,则从buffer队列中取出一个数据,并调用notify()方法通知生产者线程可以生产数据了。 在上面的示例中,使用了synchronized关键字来实现线程的同步。synchronized关键字可以确保在同一时间只有一个线程可以执行被synchronized关键字包裹的代码块。wait()方法可以让线程等待,直到被notify()方法唤醒。notify()方法可以唤醒一个等待的线程。如果有多个线程等待,则只有一个线程会被唤醒。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值