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

上传人:pu****.1 文档编号:512917894 上传时间:2023-07-24 格式:DOC 页数:7 大小:26.50KB
返回 下载 相关 举报
操作系统读者写者问题算法_第1页
第1页 / 共7页
操作系统读者写者问题算法_第2页
第2页 / 共7页
操作系统读者写者问题算法_第3页
第3页 / 共7页
操作系统读者写者问题算法_第4页
第4页 / 共7页
操作系统读者写者问题算法_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《操作系统读者写者问题算法》由会员分享,可在线阅读,更多相关《操作系统读者写者问题算法(7页珍藏版)》请在金锄头文库上搜索。

1、精选优质文档-倾情为你奉上读者优先public class MYRW public static void main(String args)CubbyHole c=new CubbyHole();Reader r1=new Reader(1,c);Reader r2=new Reader(2,c);Reader r3=new Reader(3,c);Reader r4=new Reader(4,c);Writer w1=new Writer(1,c);Writer w2=new Writer(2,c);r1.start();tryThread.sleep(1000);r2.start();T

2、hread.sleep(1000);w1.start();Thread.sleep(1000);r3.start();Thread.sleep(1000);w2.start();Thread.sleep(1000);r4.start();Thread.sleep(1000);catch(Exception e)class CubbyHoleint readCount,writeCount;boolean sourceReading=false,sourceWriting=false;public CubbyHole()readCount=0;writeCount=0;sourceReading

3、=false;sourceWriting=false;public synchronized int startRead()while(writeCount0&readCount=0)System.out.println(reader is waiting);try wait(); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();readCount+;if(readCount0)sourceReading=true;return readCount;public synchr

4、onized int endRead()readCount-;if(readCount=0)sourceReading=false;notifyAll();/System.out.println(one reader finished reading);return readCount;public synchronized void startWrite()writeCount+;while(sourceReading=true|sourceWriting=true)System.out.println(Writer is waiting);try wait(); catch (Interr

5、uptedException e) / TODO Auto-generated catch blocke.printStackTrace();sourceWriting=true;public synchronized void endWrite()writeCount-;sourceWriting=false;/System.out.println(one writer finished writing);notifyAll();class Reader extends ThreadCubbyHole c;int rNumber;public Reader(int r,CubbyHole c

6、)rNumber=r;this.c=c;public void run()int x;if(true)/System.out.println(读线程+rNumber+正在休眠);try /Thread.sleep(3000);System.out.println(读线程+rNumber+申请读);x=c.startRead();/Thread.sleep(3000);System.out.println(读线程+rNumber+正在读,共有+c.readCount+个线程正在读);Thread.sleep(2500);System.out.println(x=c.endRead()+读线程+r

7、Number+已经完成读操作,h还有+x+个线程在读); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();class Writer extends Threadint wNumber;CubbyHole c;public Writer(int w,CubbyHole c)wNumber=w;this.c=c;public void run()int x;if(true)/System.out.println(写线程+wNumber+正在休眠);try /Thread.slee

8、p(3000);System.out.println(写线程+wNumber+申请写);c.startWrite();/Thread.sleep(2500);System.out.println(写线程+wNumber+正在写);Thread.sleep(2500);c.endWrite();System.out.println(写线程+wNumber+已经完成写操作); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();写者优先public class MYRW public

9、 static void main(String args)CubbyHole c=new CubbyHole();Reader r1=new Reader(1,c);Reader r2=new Reader(2,c);Reader r3=new Reader(3,c);Reader r4=new Reader(4,c);Writer w1=new Writer(1,c);Writer w2=new Writer(2,c);r1.start();tryThread.sleep(1000);r2.start();Thread.sleep(1000);w1.start();Thread.sleep

10、(1000);r3.start();Thread.sleep(1000);w2.start();Thread.sleep(1000);r4.start();Thread.sleep(1000);catch(Exception e)class CubbyHoleint readCount,writeCount;boolean sourceReading=false,sourceWriting=false;public CubbyHole()readCount=0;writeCount=0;sourceReading=false;sourceWriting=false;public synchro

11、nized int startRead()while(writeCount0)System.out.println(reader is waiting);try wait(); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();readCount+;if(readCount0)sourceReading=true;return readCount;public synchronized int endRead()readCount-;if(readCount=0)sourceR

12、eading=false;notifyAll();/System.out.println(one reader finished reading);return readCount;public synchronized void startWrite()writeCount+;while(sourceReading=true|sourceWriting=true)System.out.println(Writer is waiting);try wait(); catch (InterruptedException e) / TODO Auto-generated catch blocke.

13、printStackTrace();sourceWriting=true;public synchronized void endWrite()writeCount-;sourceWriting=false;/System.out.println(one writer finished writing);notifyAll();class Reader extends ThreadCubbyHole c;int rNumber;public Reader(int r,CubbyHole c)rNumber=r;this.c=c;public void run()int x;if(true)/System.out.println(

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号