📄 producer.java
字号:
/**
* 生产者
*
*/
public class Producer implements Runnable{
private Semaphore mutex,full,empty;
private Buffer buf;
String name;
public Producer(String name,Semaphore mutex,Semaphore full,Semaphore empty,Buffer buf){
this.mutex = mutex;
this.full = full;
this.empty = empty;
this.buf = buf;
this.name = name;
}
public void run(){
while(true){
empty.p();
mutex.p();
System.out.println(name+" inserts a new product into "+buf.nextEmptyIndex);
buf.nextEmptyIndex = (buf.nextEmptyIndex+1)%buf.size;
mutex.v();
full.v();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -