搜索
首页 电脑/网络 软件 系统软件

Java多线程生产者与消费者 是什么?

Java多线程生产者与消费者 是什么?

全部回答

2018-03-31

44 0
    模拟生产者与消费者实例,生产者生产一个产品,消费者就消费一个产品 ,然后生产者再生产,消费者再消费 ***********************核心方法类**************** package test。
  com; class Queue // key { int value; boolean bFull = false; public synchronized void put(int i) { if (!bFull) { value = i; bFull = true; notify();// 必须用在synchronized } try { wait();// 必须捕获异常 } catch (InterruptedException e) { // TODO Auto-generated catch block e。
    printStackTrace(); } } public synchronized int get() { if (!bFull) try { wait();//进入 } catch (InterruptedException e) { // TODO Auto-generated catch block e。
    printStackTrace(); } bFull = false; notify(); return value; } } ***************************************************************** ************************生产者类******************************** package test。
    com; class Producter extends Thread { Queue q; Producter (Queue q) { this。q=q; } public void run() { System。
  out。println("********producter****start*****"); for(int i=1;i<10;i++) { System。  out。
  println("producter :"+i); q。put(i); }。

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

电脑/网络
系统软件
硬件
电脑装机
程序设计
互联网
操作系统/系统故障
笔记本电脑
反病毒
百度
软件
软件
系统软件
多媒体软件
办公软件
网络软件
图像处理软件
系统软件
系统软件
举报
举报原因(必选):
取消确定举报