java生产者消费者流程图_Java-经典消费者和生产者代码(面试题)

生产者代码

import java.util.Stack;

public class Consumer extends Thread{

private Stackstack;

Consumer( Stackstack){

super();

this.stack = stack;

}

@Override

public void run() {

synchronized (stack) {

while(true){

if(!stack.empty()){

int pop = stack.pop();

System.out.println("pop"+pop);

stack.notify();

}else{//等待

try {

stack.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

}

消费者代码

package syncreadandwrite;

import java.util.Stack;

public class Producer extends Thread{

private Stackstack;

Producer( Stackstack){

super();

this.stack = stack;

}

@Override

public void run() {

int i=0;

synchronized (stack) {

while(i<500){

if(stack.empty()){//为空,生产数据.

stack.push(i);

System.out.println("push"+i);

i++;

stack.notify();

}else{//不为空,等待数据被消费

try {

stack.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

}

启动类

package syncreadandwrite;

import java.util.Stack;

public class DemoClass {

public static void main(String[] args) throws InterruptedException {

Stackstack = new Stack();

Producer p = new Producer(stack);

Consumer c = new Consumer(stack);

p.start();

c.start();

}

}

结果

push469

pop469

push470

pop470

push471

pop471

push472

pop472

push473

pop473

push474

pop474

push475

pop475

push476

pop476

push477

pop477

push478

pop478

push479

pop479

push480

pop480

push481

pop481

push482

pop482

push483

pop483

push484

pop484

push485

pop485

push486

pop486

push487

pop487

push488

pop488

push489

pop489

push490

pop490

push491

pop491

push492

pop492

push493

pop493

push494

pop494

push495

pop495

push496

pop496

push497

pop497

push498

pop498

push499

pop499

注意:notify()和wait()方法的配合使用,并且使用这两个方法的时候必须获取锁对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package producer; import java.util.Vector;//输入java矢量 class SyncStack {//实现堆栈功能,不能同时读写 private Vector buffer//私人接口向量缓冲区 = new Vector //新建向量 (400,200); char contents; private boolean ava=false; public synchronized char get() {//出栈 while (ava==false) //如果生产者还没有产生字符就一直等待 { try { this.wait(); } catch (InterruptedException e)//当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常 { e. printStackTrace(); } } ava=false; notifyAll(); return contents; } public synchronized void push(char c) { while(ava==true){ try { wait(); } catch (InterruptedException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } }//入栈 ava=true; this.notify();//通知其它线程把数据出栈 Character charObj = new Character(c); buffer.addElement(charObj); contents=c; } public void print(char c) { // TODO 自动生成方法存根 } } class ControlEnter {//每行输出结果的个数 public static int count = 0; void counter() { count++; if(count ==5) { System.out.println(); count = 0; } } } class Producer implements Runnable {//生产者类 private SyncStack theStack;//生产者类获得的字符都来自同步堆栈 private ControlEnter controlenter; public Producer (SyncStack s,ControlEnter ce) { theStack = s; controlenter = ce; } public void run() { char c; for (int i = 0; i < 30; i++) { c = (char)(Math.random() * 26 + 'a');//随机产生30个小写字母 theStack.push(c);//存入堆栈 System.out.print("生产者产生: " + c +" "); controlenter.counter( ); try { Thread.sleep//线程休眠 ((int)(Math.random() * 200));//以0~200ms的速度随机 } catch (InterruptedException e) //当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常 { e.printStackTrace( ); } } } } class Consumer implements Runnable {//消费者类 private SyncStack theStack;//消费者类获得的字符都来自同步堆栈 private ControlEnter controlenter; public Consumer (SyncStack s,ControlEnter ce) { theStack = s; controlenter = ce; } public void run() { char value; for (int i=0; i < 30; i++) {//从堆栈中取数,并输出 value = theStack.get();//取出生产者产生的字母 System.out.print("消费者消费: " + value+" ");//消费者输出 controlenter.counter( ); try { Thread.sleep((int) (Math.random() * 2000));//控制取数的速度:0~2s/*每读取一个字符线程就睡眠*/ } catch (InterruptedException e)//当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常 { e.printStackTrace(); } } } } public class B {//主线程总调度 public static void main(String []args) { ControlEnter ce = new ControlEnter(); SyncStack stack = new SyncStack();//下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象 Producer p1 = new Producer(stack,ce); new Thread(p1).start();//生产者线程启动 Consumer c1 = new Consumer(stack,ce); new Thread(c1).start();//消费者线程启动 } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值