customer.java

来自「这是操作系统的程序设计」· Java 代码 · 共 31 行

JAVA
31
字号
/**
 * 消费者
 *
 */
public class Customer implements Runnable{
	private Semaphore mutex,full,empty;
	private Buffer buf;
	String name;
	public Customer(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){
			full.p();
			mutex.p();
			System.out.println(name+" gets a  product from "+buf.nextFullIndex);
			buf.nextFullIndex = (buf.nextFullIndex+1)%buf.size;
			mutex.v();
			empty.v();	
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {				
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?