使用java库信号量的有界缓冲区
我现在有一个任务,我应该为有界缓冲区问题做一个信号量。我认为我的第一个代码是可以的,应该可以工作(如果我正确理解信号量,顺便说一句,我不确定) 分配的第二部分是使用java库实现信号量。我做对了吗? 我已经为作业的每个部分提供了两个代码块。我只需要验证我的代码是否正确,也许还需要一些关于java信号量库使用的信息:) 使用Java信号量:使用java库信号量的有界缓冲区,java,semaphore,Java,Semaphore,我现在有一个任务,我应该为有界缓冲区问题做一个信号量。我认为我的第一个代码是可以的,应该可以工作(如果我正确理解信号量,顺便说一句,我不确定) 分配的第二部分是使用java库实现信号量。我做对了吗? 我已经为作业的每个部分提供了两个代码块。我只需要验证我的代码是否正确,也许还需要一些关于java信号量库使用的信息:) 使用Java信号量: Semaphore semaphore = new Semaphore(1); public producer(){ semaphore.ac
Semaphore semaphore = new Semaphore(1);
public producer(){
semaphore.acquire();
putItemIntoBuffer(item);
semaphore.release();
}
public consumer(){
semaphore.acquire();
removeItemFromBuffer(item);
semaphore.release();
}
在第一个代码示例中,使用二进制信号量和计数信号量。您也应该在java实现中使用它。 我认为,应该用0初始化信号量(用于产品计数)。 当您使用1时,您可以在缓冲区仍然为空时从缓冲区中提取项目
Semaphore mutex = new Semaphore(1);
Semaphore productCount = new Semaphre(0);
public producer(){
mutex.acquire(); //entering critical section
putItemIntoBuffer(item); //produce your item in the exclusive mode
productCount.release(); //increase your product count
mutex.release(); //leave critical section
}
public consumer(){
mutex.acquire(); //entering critical section
if(productCount.availablePermits() > 0) //test if you have already poduced some items
{
removeItemFromBuffer(item); //remove your item in the exclusive mode
productCount.acquire(); //decrease your item count
}
mutex.release(); //leave critical section
}
使用锁定和条件对象
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}
好吧,虽然java部分可能会遗漏一些东西?有一个问题:信号量用于互斥。如果在putItemIntoBuffer/removeItemFromBuffer的其中一个内,它会阻塞,因为等待未满/未空,所以会出现死锁:计数器部分将阻塞互斥信号量。好的!谢谢帮了很多忙-我现在对信号量的用法有了更好的理解。
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}