看了毕向东老师的生产者消费者,就照着视频参考运行了一下,感觉还好

这个值得学习的是条理特别清晰:

ProducterConsumerDemo.java中,一个资源类Resources,生产者消费者都可以访问的到。

生产者类Producter,消费者Consumer都实现了Runnable接口,在其中的run方法中实现重载,对共享资源进行生产和消费

优化:

如果以后需要加入项目中,对ProducterConsumerDemo类中加一个构造方法,public ProducterConsumerDemo(){...},实例化对象即可调用

代码:

 class  ProducterConsumerDemo
{
public static void main(String[] args)
{
Resources r =new Resources();
Productor pro =new Productor(r);
Consumer con = new Consumer(r); Thread t1 =new Thread(pro);
Thread t2 =new Thread(con);
t1.start();
t2.start();
System.out.println("Hello World!");
}
} class Resources
{
private String name;
private int count =1;
private boolean flag =false; public synchronized void set(String name)
{
if(flag)
try{this.wait();}catch(Exception e){}
this.name = name+"--"+count++; System.out.println(Thread.currentThread().getName()+"生产者"+this.name);
flag =true;
//唤醒对方进程
this.notify(); }
public synchronized void out()
{
if(!flag)
try{this.wait();}catch(Exception e){} System.out.println(Thread.currentThread().getName()+" ....消费者...."+this.name);
flag =false;
//唤醒对方进程
this.notify(); }
} class Productor implements Runnable
{
private Resources res;
Productor(Resources res){
this.res =res;
}
public void run(){
while(true){
res.set("++商品++");
}
} } class Consumer implements Runnable
{
private Resources res;
Consumer(Resources res){
this.res =res;
}
public void run(){
while(true){
res.out();
}
} }

由于自己需要的功能是,生产者消费者对一个二维数组进行操作,所以在其基础上加了资源Resources类的成员属性。实现存取功能

算是一个框架吧,留存:

 class  ProducterConsumerArr2D
{
public static void main(String[] args)
{
Resources r =new Resources();
Productor pro =new Productor(r);
Consumer con = new Consumer(r); Thread t1 =new Thread(pro);
Thread t2 =new Thread(con);
t1.start();
t2.start();
System.out.println("Hello World!");
}
} class Resources
{
private String name;
private int count =1;
private float[][] arr2D=new float[5][5]; //为它分配5行5列的空间大小
private boolean flag =false; public synchronized void set(String name)
{
if(flag)
try{this.wait();}catch(Exception e){}
//需放在count++前面
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
arr2D[i][j]=(float)count;
}
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"生产者"+this.name); flag =true;
//唤醒对方进程
this.notify(); }
public synchronized void out()
{
if(!flag)
try{this.wait();}catch(Exception e){} System.out.println(Thread.currentThread().getName()+" ....消费者...."+this.name+"二维数组任意元素"+arr2D[2][2]);
flag =false;
//唤醒对方进程
this.notify(); }
} class Productor implements Runnable
{
private Resources res;
Productor(Resources res){
this.res =res;
}
public void run(){
while(true){
res.set("++二维数组存取++");
}
} } class Consumer implements Runnable
{
private Resources res;
Consumer(Resources res){
this.res =res;
}
public void run(){
while(true){
res.out();
}
} }

ProducterConsumerArr2D.java

当然,这个只针对支持两个互相独立的线程,如果继续加入多个线程(>2)肯定还会有资源数据出错的问题,继续学习

java线程之生产者消费者的更多相关文章

  1. 第23章 java线程通信——生产者/消费者模型案例

    第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...

  2. Java线程通信-生产者消费者问题

    线程通信示例——生产者消费者问题 这类问题描述了一种情况,假设仓库中只能存放一件产品,生产者将生产出来的产品放入仓库,消费者将仓库中的产品取走消费.假设仓库中没有产品,则生产者可以将 产品放入仓库,有 ...

  3. Java 线程池 +生产者消费者+MySQL读取300 万条数据

    1.1需求 数据库300 万条用户数据 ,遍历获取所有用户, 各种组合关联, 获取到一个新的json ,存到redis 上. 1.2 难点 数据库比较多, 不可能单线程查询所有的数据到内存. 1.3解 ...

  4. Java如何使用线程解决生产者消费者问题?

    在Java编程中,如何使用线程解决生产者消费者问题? 以下示例演示如何使用线程解决生产者消费者问题. package com.yiibai; public class ProducerConsumer ...

  5. Java多线程-同步:synchronized 和线程通信:生产者消费者模式

    大家伙周末愉快,小乐又来给大家献上技术大餐.上次是说到了Java多线程的创建和状态|乐字节,接下来,我们再来接着说Java多线程-同步:synchronized 和线程通信:生产者消费者模式. 一.同 ...

  6. java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】

    java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-[费元星Q9715234] 说明如下,不懂的问题直接我[费元星Q9715234] 1.反射的意义在于不将xml tag ...

  7. Java设计模式之生产者消费者模式

    Java设计模式之生产者消费者模式 博客分类: 设计模式 设计模式Java多线程编程thread 转载 对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的.就像学习每一门编程语言一 ...

  8. java多线程模拟生产者消费者问题,公司面试常常问的题。。。

    package com.cn.test3; //java多线程模拟生产者消费者问题 //ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品 // ...

  9. JAVA多线程之生产者 消费者模式 妈妈做面包案例

    创建四个类 1.面包类 锅里只可以放10个面包 ---装面包的容器2.厨房 kitchen 生产面包 和消费面包  最多生产100个面包3.生产者4消费者5.测试类 多线程经典案例 import ja ...

随机推荐

  1. ZOJ 3931 Exact Compression

    题目看了半小时才看懂的. 题意:首先根据给出的序列,构造出哈夫曼树,构造出来的是一棵二叉树,每个节点都有一个权值,每个节点的两个儿子只能取一个,问能否使取出来的节点权值之和刚好等于e. 这样一分析就很 ...

  2. Extjs6中的新特性

    Ext JS在Sencha框架中引入了许多新的和令人兴奋的改进.这些变化为基于所有现代浏览器.设备和屏幕尺寸带来了新的功能和可用性. 工具包(ToolKits) Ext JS 6最大的变化就是将Ext ...

  3. jqGrid的搜索框下拉

    当需要在jqGrid的搜索框里配置搜索条件时,如下拉,日期等,代码如下: datePick = function(elem) { jQuery(elem). } colNames : [ " ...

  4. mysql 数据库 切表的脚本

    #!/bin/sh host=$1 port=$2 host=${host:="localhost"}  #host没赋值,那么就赋值为localhost port=${port: ...

  5. Java对象嵌套

    1.基础篇 Java面向对象东西太深奥了,本文只是发表一点自己的见解. 首先 举个栗子!! 汽车, 我们先给汽车定义个轮胎类  有一个属性名 轮胎尺寸 /** *定义一个轮胎类 */ class Wh ...

  6. ucos移植指南

    指定堆栈数据类型(宽度) typedef unsigned int OS_STK; 指定Ucos移植方法3中保存cpu状态寄存器的变量的宽度 typedef unsigned int OS_CPU_S ...

  7. 试水MongoDB

    1)安装好后启动mongodb 服务 1_1) 建立data/db     ,保证至少有3g大小的盘 1_2) 建立log 文件夹 1_3)配置文件 内容,指定数据存放位置.日志文件位置 dbpath ...

  8. IAR for STM8 错误

    一个IAR for STM8 v1.3 的工程,换到1.4版后出现如下错误 unable to allocate space for sections/blocks with a total esti ...

  9. 写一个程序,统计自己C语言共写了多少行代码。ver2.00

    概要 完成一个程序,作用是统计一个文件夹下面所有文件的代码行数.输入是一个文件夹的绝对路径,输出是代码行数.所以此程序的新特点有两个: 统计某一文件夹下的所有文件: 可以任意指定本机硬盘上任何位置的某 ...

  10. 《剑指Offer》笔记(更新中)

    这几天为了找工作开始看<剑指offer>,到现在也大概浏览一遍了,前两天看作者博客中提到九度OJ,就去看了一下,发现上面有书上的题目,就想可以自己写代码练习一下,而不仅仅是看解题思路,毕竟 ...