有一只铁笼子,每次只能放入一只动物,猎手向笼中放入老虎,农民向笼中放入猪,动物园等待取笼中的老虎,饭店等待取笼中的猪,试用Java中的P、V操作写出能同步执行的程序.题目的实质是:两个生产者和两个消费者共享了一个仅能存放一件产品的缓冲器,生产者各自生产不同的产品,消费者各自取自己需要的产品。请高手给出程序。
解决方案 »
- Exception in thread "main" java.lang.NullPointerException
- 一道3国为主题的程序考题,感兴趣的高手进来帮忙解答下!
- 请教 :java 还原编码utf-8 转 gbk问题(实现多编码格式请求处理的web应用)
- 有关jfreechar????
- 老问题!!
- JBuild 8.0 中的applet程序中,为什么在运行时总是显示:class not found
- 关于如何调用corejava.*的问题?
- diy.texteditframe.class.getResource("openFile.gif")这段话我真不明白?
- 请问用Color类要import什么库
- 关于Applet之间如何传递信息的问题
- 有关java静态类
- spring可以为单个方法配置事务吗?
消费者,先判断是否为空。如果不为空则peek出来,检测是否是自己需要的类型,如果不是,则不消费,释放锁伪码
猎手:
syn(lock){
while(!isEmpty())
lock.wait();
put(tiger);
lock.notifyAll();
}农民:
syn(lock){
while(!isEmpty())
lock.wait();
put(pig);
lock.notifyAll();
}动物园:
syn(lock){
while(!isEmpty() || !(peek() instanceof Tiger))
lock.wait();
getTiger();
lock.notifyAll();
}
饭店:
syn(lock){
while(!isEmpty() || !(peek() instanceof Pig))
lock.wait();
getPig();
lock.notifyAll();
}
动物园和饭店的判断条件
while(isEmpty() ||
ST = 0 //
SP = 0 ////猎手进程
for(;;) {
P(SC);
put(tiger);
V(ST);
}//农夫进程
for(;;) {
P(SC);
put(pig);
V(SP);
}//动物园进程
for(;;) {
P(ST);
get(tiger);
V(SC);
}//饭店进程
for(;;) {
P(SP);
get(pig);
V(SC);
}以上是伪码实现,要真正用JAVA实现的话用wait()和notify()来实现PV操作即可
import java.util.concurrent.Semaphore;public class SynchronizeTest {
//看了文档之后才知道JAVA1.5以后已经有Semaphore了
//对于这个例子由于笼子容量为1,所以只用wait和notify也是可以的,笼子容量更大时就要用到信号量了
Semaphore sCage = new Semaphore(1);
Semaphore sTiger = new Semaphore(1);
Semaphore sPig = new Semaphore(1);
Random rnd = new Random(System.currentTimeMillis()); public void test() {
try {
//初始时笼子里没有动物
sTiger.acquire();
sPig.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new HunterThread().start();
new FarmerThread().start();
new ZooThread().start();
new HotelThread().start();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new SynchronizeTest().test();
} class HunterThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
for (;;) {
try {
//为了方便看输出,加入延时
System.out.println("紧张激烈地打猎中...");
sleep(rnd.nextInt(5000));
sCage.acquire();
System.out.println("========放入老虎========");
sTiger.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} class FarmerThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
for (;;) {
try {
System.out.println("紧张激烈地喂猪中...");
sleep(rnd.nextInt(5000));
sCage.acquire();
System.out.println("========放入猪========");
sPig.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} class ZooThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
for (;;) {
try {
sTiger.acquire();
System.out.println("========取出老虎========");
sCage.release();
System.out.println("老虎表演中...");
sleep(rnd.nextInt(5000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} class HotelThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
for (;;) {
try {
sPig.acquire();
System.out.println("========取出猪========");
sCage.release();
System.out.println("红烧肉制作中...");
sleep(rnd.nextInt(5000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}