返回信息流//semaphore会不会引起一些同步的问题(重入),求大家的看法...
package learn;
import java.util.concurrent.Semaphore;
class applePlate{
static Semaphore apple = new Semaphore(2);
static Semaphore space = new Semaphore(3);
}
class Eater implements Runnable{
String name;
Object o;
Eater(String name, Object o){
this.name = name;
this.o = o;
}
public void run(){
int count = 4;
while(count-- > 0){
try{
applePlate.apple.acquire();
}catch(InterruptedException e){
throw new RuntimeException(e);
}
applePlate.space.release();
System.out.println("Eater " + name +" eat"+" :left "
+ applePlate.apple.availablePermits());
}
}
}
class Producer implements Runnable{
String name;
Object o;
Producer(String name, Object o){
this.name = name;
this.o = o;
}
public void run(){
int count = 3;
while(count-- > 0){
try{
applePlate.space.acquire();
}catch(InterruptedException e){
throw new RuntimeException(e);
}
applePlate.apple.release();
System.out.println("Producer " + name +" produce"+" :left "
+ applePlate.apple.availablePermits());
}
}
}
class Go {
public static void main(String args[]){
Object o = new Object();
Eater e1 = new Eater("eee", o), e2 = new Eater("eee2", o);
Producer p1 = new Producer("ppp", o), p2 = new Producer("ppp2", o);
Thread th1 = new Thread(e1);
Thread th2 = new Thread(e2);
Thread th3 = new Thread(p1);
Thread th4 = new Thread(p2);
th1.start();
th2.start();
th3.start();
th4.start();
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #24151同步于 2013/1/1
Java机器人发帖
【提问】semaphore会不会引起一些同步的问题(重入)
mubings
2013/1/1镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。