資源簡(jiǎn)介
進(jìn)程同步模擬設(shè)計(jì)--生產(chǎn)者和消費(fèi)者問(wèn)題 進(jìn)程調(diào)度同步異步
代碼片段和文件信息
?class?Producer?implements?Runnable
{
private?Semaphore?mutexfullempty;
private?Buffer?buf;
String?name;
public?Producer(String?nameSemaphore?mutexSemaphore?fullSemaphore?emptyBuffer?buf)
{
this.name?=?name;
this.mutex?=?mutex;
this.full?=?full;
this.empty?=?empty;
this.buf?=?buf;
}
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();
}
}
}
}
class?Customer?implements?Runnable
{
private?Semaphore?mutexfullempty;
private?Buffer?buf;
String?name;
public?Customer(String?nameSemaphore?mutexSemaphore?fullSemaphore?emptyBuffer?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();
S
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????2868??2010-01-22?21:38??Test.java
-----------?---------??----------?-----??----
?????????????????2868????????????????????1
- 上一篇:PReS指令集詳細(xì)說(shuō)明教程
- 下一篇:飛思卡爾K60 ADC
評(píng)論
共有 條評(píng)論