生产消费模式
2015-09-07 15:37
239 查看
package com.phone.week5.day3;
public class MasterStudentDemo {
}
// 篮子
class LanZi {
// private boolean flag=false;
private int number = 6;
// 馒头增加的方法
public synchronized void zengJia() {
while (number >= 6) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number++;
System.out.println(Thread.currentThread().getName() + “放了一个馒头,篮子里已经有:”
+ number + “个馒头了”);
this.notifyAll();
}
package com.phone.week5.day3;
/**
* 有一个仓库放字符的,它有一个生产字符的方法,也有一个拿字符的方法(仓库里只能有一个字符)
* 有一个生产者,可以调用仓库里生产字符的方法
* 有一个消费者,可以调用仓库里拿字符的方法
* 两个人不能同时生产或拿
*
*1.创建一个共享资源类
*2.创建生产者
*3.创建消费者
*5.测试
*/
public class Test {
}
//生产者类
class Pro extends Thread{
private ShareDate sd;
public Pro(ShareDate sd){
this.sd = sd;
}
@Override
public void run() {
for (char c = ‘a’;c<=’d’;c++) {
try {
Thread.sleep((int)(Math.random()*3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
sd.pushDate(c);
}
}
}
//消费者类
class Custor extends Thread{
private ShareDate sd;
public Custor(ShareDate sd){
this.sd = sd;
}
@Override
public void run() {
char c ;
do{
try {
Thread.sleep((int)(Math.random()*3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//资源类
class ShareDate{
private char c; //有字符
//生产者与消费者互相通知的一个标识
private boolean flag = false; //表示没有字符,消费者不能消费
}
public class MasterStudentDemo {
public static void main(String[] args) { final LanZi lz = new LanZi();// 创建一个篮子对象,它也是相当于锁 new Thread(new Runnable() { @Override public void run() { new Master(lz).fang();// 创建一个师傅对象 } }, "师父A").start(); new Thread(new Runnable() { @Override public void run() { new Master(lz).fang();// 创建一个师傅对象 } }, "师父B").start(); new Thread(new Runnable() { @Override public void run() { new Student(lz).na();// 创建第一个学生对象 } }, "学生A").start(); new Thread(new Runnable() { @Override public void run() { new Student(lz).na();// 创建第一个学生对象 } }, "学生B").start(); new Thread(new Runnable() { @Override public void run() { new Student(lz).na();// 创建第一个学生对象 } }, "学生C").start(); } // 师傅类 static class Master { LanZi lz; public Master(LanZi lz) { super(); this.lz = lz; } public void fang() { for (int i = 1; i <= 3; i++) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } lz.zengJia();// 增加 } } } // 学生类 static class Student { LanZi lz; public Student(LanZi lz) { super(); this.lz = lz; } public void na() { for (int i = 1; i <= 3; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } lz.jianShao(); } } }
}
// 篮子
class LanZi {
// private boolean flag=false;
private int number = 6;
// 馒头增加的方法
public synchronized void zengJia() {
while (number >= 6) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number++;
System.out.println(Thread.currentThread().getName() + “放了一个馒头,篮子里已经有:”
+ number + “个馒头了”);
this.notifyAll();
} // 馒头减少的方法 public synchronized void jianShao() { while (number <= 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } number--; System.out.println(Thread.currentThread().getName() + "学生拿了一个,篮子里还剩:" + number + "个"); this.notifyAll(); }
}
package com.phone.week5.day3;
/**
* 有一个仓库放字符的,它有一个生产字符的方法,也有一个拿字符的方法(仓库里只能有一个字符)
* 有一个生产者,可以调用仓库里生产字符的方法
* 有一个消费者,可以调用仓库里拿字符的方法
* 两个人不能同时生产或拿
*
*1.创建一个共享资源类
*2.创建生产者
*3.创建消费者
*5.测试
*/
public class Test {
public static void main(String[] args) { ShareDate sd = new ShareDate(); Pro p = new Pro(sd); Custor c = new Custor(sd); p.start(); c.start(); }
}
//生产者类
class Pro extends Thread{
private ShareDate sd;
public Pro(ShareDate sd){
this.sd = sd;
}
@Override
public void run() {
for (char c = ‘a’;c<=’d’;c++) {
try {
Thread.sleep((int)(Math.random()*3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
sd.pushDate(c);
}
}
}
//消费者类
class Custor extends Thread{
private ShareDate sd;
public Custor(ShareDate sd){
this.sd = sd;
}
@Override
public void run() {
char c ;
do{
try {
Thread.sleep((int)(Math.random()*3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
c=sd.getData(); }while(c!='d'); }
}
//资源类
class ShareDate{
private char c; //有字符
//生产者与消费者互相通知的一个标识
private boolean flag = false; //表示没有字符,消费者不能消费
//生产的方法 public synchronized void pushDate(char c){ //表示可以生产 if(flag){ System.out.println("消费者还没有消费,因此生产者还不能生产"); try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.c = c; //生产字符 flag = true; //表示通过消费者来消费 this.notify(); System.out.println("生产者已经生产完字符:"+c+"请消费者来消费"); } //拿字符的方法 public synchronized char getData(){ if(!flag){ System.out.println("生产者还没有生产,因此消费者还不能消费"); try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); System.out.println("消费者已经拿到字符:"+c+"请生产者来生产"); return c; }
}
相关文章推荐
- jQuery编程规范与最佳实践(附带一些个人的笔记)
- require.js优化器
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
- Hibernate学习总结(一)
- 使用Haproxy搭建web群集
- html5 audio video output
- python标识符
- mobileconfig文件的签名和认证(signed、verified)
- 多线程环境下生成随机数
- maven核心,pom.xml详解
- ArcGIS for Android地图去除水印
- AutolayoutVFL
- 初探 performance – 监控网页与程序性能
- Android View.onMeasure方法的理解
- 写在ie6里的点滴
- FastJson使用详解Demo
- 【Java】JDK6和JDK7中String的substring()方法及其差异
- (3)分布式下的爬虫Scrapy应该如何做-递归爬取方式,数据输出方式以及数据库链接
- PHP找回密码机制流程
- 时间不止我们不散