关闭

生产消费模式的几种实现

51人阅读 评论(0) 收藏 举报

直接上代码!!

第一种

package test;


import java.util.Random;




public class Demo1 {
//基于yield退让
public static void main(String[] args) {
Send send = new Send();
Reci rec = new Reci(send);
Thread t1 = new Thread(send);
t1.setDaemon(true);//当只有t1线程,程序退出(守护线程)
Thread t2 = new Thread(rec);
t2.setPriority(6);
t2.setName("r2");
Thread t3 = new Thread(rec);
t3.setPriority(7);
t3.setName("r3");
Thread t4 = new Thread(rec);
t4.setPriority(8);
t4.setName("r4");
Thread t5 = new Thread(rec);
t5.setPriority(10);
t5.setName("r5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class Send extends Thread{
boolean flag;
int value;
@Override
public   void run() {
// TODO Auto-generated method stub
for (;;) {
while(flag){
Thread.yield();
}
value=new Random().nextInt();
System.out.println("制作好食物"+value);
flag=true;
}
}
}
class Reci extends Thread{
private Send send;


public Reci(Send send) {
super();
this.send = send;
}
@Override
public synchronized void run() {
// TODO Auto-generated method stub
System.out.println("线程"+Thread.currentThread().getName()+"开吃");
while(!send.flag){
Thread.yield();//下次接着运行,时间片轮换,这个对象锁并没有被放弃,只要是这个对象下次接着来,也就是说在当前线程重新回到CPU上运行之前,其他线程都不能使用run方法。
}
System.out.println(Thread.currentThread().getName()+

"已经消费食物"+send.value);
send.flag=false;
System.out.println(Thread.currentThread().getName()+"吃完");
}


}




/*开吃
制作好食物-1900039954
r2已经消费食物-1900039954
吃完
开吃
制作好食物-851625431  //被迫做食物,食客在等
r5已经消费食物-851625431
吃完
开吃
制作好食物1038626377
r4已经消费食物1038626377
吃完
制作好食物1175664726  //主动做食物,现在没客人(客人啥都吃不挑食)
开吃
r3已经消费食物1175664726
吃完
制作好食物1727535480
*/





第二种

package test;


import java.util.Random;


public class Demo2 {
public static void main(String[] args) {
Send1 send = new Send1();
Reci1 rec = new Reci1(send);
Thread t1 = new Thread(send);
t1.setDaemon(true);
Thread t2 = new Thread(rec);
t2.setPriority(10);
t2.setName("r2");
Thread t3 = new Thread(rec);
t3.setPriority(10);
t3.setName("r3");
Thread t4 = new Thread(rec);
t4.setPriority(10);
t4.setName("r4");
Thread t5 = new Thread(rec);
t5.setPriority(10);
t5.setName("r5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}


}
class Send1 implements Runnable{
boolean flag;
int value;
public  void run(){
synchronized (this) {
for (;;) {
while(flag){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
value=new Random().nextInt();
System.out.println("已经生产食物"+value);
flag=true;
this.notifyAll();
}
}
}

}
class Reci1 implements Runnable{
private Send1 send;


public Reci1(Send1 send) {
super();
this.send = send;
}
public  void run(){
synchronized (this) {//保证同步
synchronized (send) {//因为下面要调用send,wait(),所以锁的对象是send
System.out.println("开吃");
while(!send.flag){
try {
send.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"已经消费食物:"+send.value);
send.flag=false;
send.notifyAll();
System.out.println("吃完");
}

}

}
}
/*已经生产食物964920772  
开吃
r2已经消费食物:964920772
吃完
开吃
已经生产食物-1103227087  //被迫生产客户在等aaa
r5已经消费食物:-1103227087
吃完
已经生产食物-14069138   //主动生产,客户不挑食,啥都吃
开吃
r4已经消费食物:-14069138
吃完
已经生产食物2082765831
开吃
r3已经消费食物:2082765831
吃完
已经生产食物1728566631*/





第三种

package test;


import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Demo3 {
public static void main(String[] args) {
final Business business = new Business();
Thread send = new Thread(){
public void run() {
business.send();
};
};
send.setDaemon(true);
send.start();

Thread reci1 = new Thread(){
public void run() {
business.reci();;
};
};
reci1.setName("r1");
Thread reci2 = new Thread(){
public void run() {
business.reci();;
};
};
reci2.setName("r2");
Thread reci3 = new Thread(){
public void run() {
business.reci();;
};
};
reci3.setName("r3");
Thread reci4 = new Thread(){
public void run() {
business.reci();;
};
};
reci4.setName("r4");

reci1.start();
reci2.start();
reci3.start();
reci4.start();





}}
class Business{
Integer value=new Integer(0);
Boolean flag=new Boolean(false);
Object o=new Object();//什么用?用于调用wait、nitify
public  void send(){
for (;;) {
synchronized (o) {
while(flag){
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
value=new Integer(new Random().nextInt());
System.out.println("已经生产食物"+value);
flag=true;
o.notifyAll();
}

}
}
public synchronized void reci(){
synchronized (o) {
System.out.println(Thread.currentThread().getName()+"开吃");
while(!flag){
try {
o.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"已经消费食物:"+value);
flag=false;
System.out.println(Thread.currentThread().getName()+"吃完");
o.notifyAll();
}

}
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:954次
    • 积分:87
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条