操作系统读者写者问题算法.doc

操作系统读者写者问题算法.doc

ID:53564633

大小:45.00 KB

页数:7页

时间:2020-04-04

操作系统读者写者问题算法.doc_第1页
操作系统读者写者问题算法.doc_第2页
操作系统读者写者问题算法.doc_第3页
操作系统读者写者问题算法.doc_第4页
操作系统读者写者问题算法.doc_第5页
资源描述:

《操作系统读者写者问题算法.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、读者优先publicclassMYRW{publicstaticvoidmain(String[]args){CubbyHolec=newCubbyHole();Readerr1=newReader(1,c);Readerr2=newReader(2,c);Readerr3=newReader(3,c);Readerr4=newReader(4,c);Writerw1=newWriter(1,c);Writerw2=newWriter(2,c);r1.start();try{Thread.sleep(1000);r2.start();Thr

2、ead.sleep(1000);w1.start();Thread.sleep(1000);r3.start();Thread.sleep(1000);w2.start();Thread.sleep(1000);r4.start();Thread.sleep(1000);}catch(Exceptione){}}}classCubbyHole{intreadCount,writeCount;booleansourceReading=false,sourceWriting=false;publicCubbyHole(){readCount=0

3、;writeCount=0;sourceReading=false;sourceWriting=false;}publicsynchronizedintstartRead(){while(writeCount>0&&readCount==0){System.out.println("readeriswaiting");try{wait();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}readCount++;if(readC

4、ount>0)sourceReading=true;returnreadCount;}publicsynchronizedintendRead(){readCount--;if(readCount==0)sourceReading=false;notifyAll();//System.out.println("onereaderfinishedreading");returnreadCount;}publicsynchronizedvoidstartWrite(){writeCount++;while(sourceReading==true

5、

6、

7、sourceWriting==true){System.out.println("Writeriswaiting");try{wait();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}sourceWriting=true;}publicsynchronizedvoidendWrite(){writeCount--;sourceWriting=false;//System.out.println("onewriterfi

8、nishedwriting");notifyAll();}}classReaderextendsThread{CubbyHolec;intrNumber;publicReader(intr,CubbyHolec){rNumber=r;this.c=c;}publicvoidrun(){intx;if(true){//System.out.println("读线程"+rNumber+"正在休眠");try{//Thread.sleep(3000);System.out.println("读线程"+rNumber+"申请读");x=c.star

9、tRead();//Thread.sleep(3000);System.out.println("读线程"+rNumber+"正在读,共有"+c.readCount+"个线程正在读");Thread.sleep(2500);System.out.println((x=c.endRead())+"读线程"+rNumber+"已经完成读操作,h还有"+x+"个线程在读");}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}cla

10、ssWriterextendsThread{intwNumber;CubbyHolec;publicWriter(intw,CubbyHolec){wNumber=w;this.

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。