您的位置:首页 > 其它

生产消费模式

2015-09-07 15:37 239 查看
package com.phone.week5.day3;

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;
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: