操作系统实验报告readerwriter.docx
《操作系统实验报告readerwriter.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告readerwriter.docx(31页珍藏版)》请在冰豆网上搜索。
操作系统实验报告readerwriter
操
作
系
统
实
验
报
告
实验题目:
ReaderWriter实验
一.实验目的:
1.通过编写和调试程序以加深对进程、线程管理方案的理解
2.熟悉Windows多线程程序设计方法
二.实验原理:
读者-写者允许多个读者同时读一个数据对象,因为读文件不会使数据发生混乱,但不允许一个写者进程与其他读者进程或写者进程同时访问该数据对象。
文件是各个进程能互斥访问临界资源,读者进程和写者进程之间互斥。
在读者进程中,可以有多个读者读数据库,在读者进程的计数要互斥,避免发生错误,同时注意当第一个读者进程读时,一定要封锁写者进程。
当读者进程逐渐撤离时,要针对计数变量进行互斥操作,若当前为最后一个读者进程,读完后应唤醒写者进程。
所以应该分四类可能性:
1.读者优先权比写者高,并且不用调配。
2.在一个读者已经占有文件的时候,全体读者的优先权才比写者高
3.写者的优先权比读者的优先权高
4.所有写者的和所有读者有相同的优先权
三.实验代码:
DataBaseBuffer:
importjava.awt.*;
publicclassDataBaseBufferextendsCanvas{
int[]readWantQ;
int[]writerWantQ;
String[]wantQ;
intframeDelay=1560;
privateintwriterID;
privateintreaderCount;
privateintwantCount;
privateintreaderWantCount,writerWantCount;
privateintwriterCount;
privateintwn,rn;//thenumberofreadersandwriters
privateintreadTop,readBottom,writerTop,writerBottom;
privateintwantTop,wantBottom;
privateFontfont;
privateFontMetricsfm;
privatebooleanreadLock=false;
privatebooleanwriteLock=false;
publicDataBaseBuffer(){
resize(500,300);
setBackground(Color.white);
font=newFont("TimesRoman",Font.BOLD,18);
fm=getFontMetrics(font);
}
publicvoidsetSize(intreaderN,intwriterN)
{
rn=readerN;
wn=writerN;
readTop=readBottom=writerTop=writerBottom=0;
readerCount=writerCount=readerWantCount=writerWantCount=0;
wantTop=wantBottom=0;
wantCount=0;
writerID=0;
readLock=false;
writeLock=false;
wantQ=newString[rn+wn];
writerWantQ=newint[wn];
readWantQ=newint[rn];
repaint();
publicsynchronizedvoidenterQueue(Strings,intid)
wantQ[wantTop]=s+id;
wantTop++;
wantCount++;
publicsynchronizedvoiddequeue(Strings,intid)
Stringstr;
str=s+id;
while(!
wantQ[0].equals(str))
try{wait();}catch(InterruptedExceptione){}
for(inti=0;i<(wantTop-1);i++)
wantQ[i]=wantQ[i+1];
wantTop--;
wantCount--;
publicsynchronizedvoidchangePosition(Strings,intid)
Stringtmp;
intpos=0;//tofindthepostingof1stwriter
Stringwtr;
wtr=s+id;
(wantQ[pos].equals(wtr)))
pos++;
for(inti=0;i{System.out.println(wantQ[i]);}str=wantQ[pos];for(inti=pos;i>0;i--){wantQ[i]=wantQ[i-1];}wantQ[0]=str;repaint();for(inti=0;i{System.out.println(wantQ[i]);}}publicsynchronizedbooleanhasWriterWant(){return(writerWantCount>0);}publicsynchronizedbooleanhasReaderWant(){return(readerWantCount>0);}publicsynchronizedvoidacquireReadLock(ReaderWriterAppletapplet,intid){readWantQ[readTop]=id;//numsistheindexforreadWantQreadTop=(readTop+1)%rn;readerWantCount++;repaint();notifyAll();enterQueue("R",id);applet.r[id].status=1;//wantinapplet.mc.println(applet.r[id].status,"r",id);try{applet.r[id].sleep(frameDelay);}catch(InterruptedExceptione){}if(applet.writerPriority){while(hasWriterWant()||writeLock){applet.r[id].status=3;applet.mc.println(applet.r[id].status,"r",id);try{wait();}catch(InterruptedExceptione){}}//endofwhileloop}//endofifstatementelseif(applet.readerPriority){while(readWantQ[readBottom]!=id){try{wait();}catch(InterruptedExceptione){}}changePosition("R",id);}else{applet.r[id].status=3;applet.mc.println(applet.r[id].status,"r",id);while(!wantQ[wantBottom].equals("R"+id)){try{wait();}catch(InterruptedExceptione){}}}while(writeLock)//ifthereisanywriteriswriting{applet.r[id].status=3;applet.mc.println(applet.r[id].status,"r",id);try{wait();}catch(InterruptedExceptione){}}if(readLock==false){readLock=true;notifyAll();}readBottom=(readBottom+1)%rn;readerWantCount--;dequeue("R",id);readerCount++;repaint();applet.r[id].status=2;applet.mc.println(applet.r[id].status,"r",id);System.out.println("Reader"+id+"isreading");notifyAll();}publicsynchronizedbooleanhasReader(){return(readerCount>0);}publicsynchronizedvoidreleaseReadLock(ReaderWriterAppletapplet,intid){readerCount--;notifyAll();if(!hasReader()){readLock=false;notifyAll();applet.r[id].status=4;applet.mc.println(applet.r[id].status,"r",id);}repaint();}publicsynchronizedvoidacquireWriteLock(ReaderWriterAppletapplet,intid){writerWantQ[writerTop]=id;writerTop=(writerTop+1)%wn;writerWantCount++;notifyAll();repaint();enterQueue("W",id);applet.w[id].status=1;//wantinapplet.mc.println(applet.w[id].status,"w",id);try{applet.w[id].sleep(frameDelay);}catch(InterruptedExceptione){} if(applet.writerPriority){while(writerWantQ[writerBottom]!=id){try{wait();}catch(InterruptedExceptione){}}changePosition("W",id);while(readLock||writeLock){try{wait();}catch(InterruptedExceptione){}}}elseif(applet.readerPriority){while(!(wantQ[wantBottom].equals("W"+id))||hasReaderWant()||readLock||writeLock){try{wait();}catch(InterruptedExceptione){}}System.out.println("Writer"+id+"moveforward");}else{while(!(wantQ[wantBottom].equals("W"+id))){try{wait();}catch(InterruptedExceptione){}}while(readLock||writeLock){try{wait();}catch(InterruptedExceptione){}}}writeLock=true;System.out.println("Writer"+id+"Gotthelock******");notifyAll();dequeue("W",id);writerBottom=(writerBottom+1)%wn;writerID=id;writerWantCount--;writerCount++;notifyAll();repaint();applet.w[id].status=2;applet.mc.println(applet.w[id].status,"w",id);}publicsynchronizedvoidreleaseWriteLock(ReaderWriterAppletapplet,intid){System.out.println("Writer"+id+"releasedthelock");writerCount--;writerID=0;writeLock=false;notifyAll();repaint();} publicvoidclear(){writerBottom=writerTop=0;readBottom=readTop=0;writerWantCount=0;readerWantCount=0;readerCount=0;writerCount=0;readLock=writeLock=false;writerWantQ=newint[wn];readWantQ=newint[rn];wantQ=newString[wn+rn];wantTop=wantBottom=0;}publicvoidpaint(Graphicsg){intxpos=630;intypos=5;g.setFont(newFont("TimesRoman",Font.BOLD,11));g.setColor(Color.green);g.draw3DRect(xpos,ypos,10,10,true);g.fillRect(xpos,ypos,10,10);g.drawString("Reading",xpos+15,ypos+10);g.setColor(Color.red);g.draw3DRect(xpos,ypos+14,10,10,true);g.fillRect(xpos,ypos+14,10,10);g.drawString("Writing",xpos+15,ypos+25);g.setColor(Color.blue);g.draw3DRect(xpos,ypos+28,10,10,true);g.fillRect(xpos,ypos+28,10,10);g.drawString("Empty",xpos+15,ypos+40);g.setFont(newFont("TimesRoman",Font.BOLD,14));g.setColor(Color.blue);xpos=40;ypos=50;g.drawString("WaitingQueue",xpos-5,ypos-20);inti=wantBottom;for(intj=0;j{if(wantQ[i].equals("W1")||wantQ[i].equals("W2")||wantQ[i].equals("W3")||wantQ[i].equals("W4")||wantQ[i].equals("W5")){g.setColor(Color.red);g.drawString(wantQ[i],xpos+450-30*j,ypos-18);g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);}if(wantQ[i].equals("R1")||wantQ[i].equals("R2")||wantQ[i].equals("R3")||wantQ[i].equals("R4")||wantQ[i].equals("R5")){g.setColor(Color.green);g.drawString(wantQ[i],xpos+450-30*j,ypos-18);g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);}i=(i+1)%(wn+rn);}if(readLock)g.setColor(Color.green);elseif(writeLock)g.setColor(Color.red);elseg.setColor(Color.blue);g.draw3DRect(xpos+250,ypos+20,100,100,true);g.fillRect(xpos+250,ypos+20,100,100);if(readLock){g.setColor(Color.black);g.drawString("Reading",xpos+270,ypos+60);}elseif(writeLock){g.setColor(Color.black);g.drawString("W"+Integer.toString(writerID),xpos+280,ypos+45);g.drawString("Writing",xpos+270,ypos+60);}}}MessageCanvas:importjava.awt.*;classMessageCanvasextendsCanvas{privateFontfont;privateFontMetricsfm;privateint[]writerStatus;privateint[]readerStatus;privateintmsgHeight;privateintmsgWidth;privateintpn,cn;privateintframeDelay=256;publicMessageCanvas(){resize(size().width,50);setBackground(Color.green);font=newFont("TimesRoman",1,18);fm=getFontMetrics(font);msgHeight=fm.getHeight();}publicvoidsetMessage(intwriterN,intreaderN){pn=writerN;cn=readerN;writerStatus=newint[pn+1];readerStatus=newint[cn+1];repaint();}voidprintln(Strings){msgWidth=fm.stringWidth(s);repaint();}voidprintln(ints,Stringst,intid){if(st.equals("w"))writerStatus[id]=s;elsereaderStatus[id]=s;repaint();}voidprintln(ints,intnumber,Stringst,intid){if(st.equals("w")){writerStatus[id]=s;}else{readerStatus[id]=s;}repaint();}publicvoidpaint(Graphicsg){g.setFont(font);intxpos=60;intypos=40;g.drawString("StatusofReaders:
System.out.println(wantQ[i]);
str=wantQ[pos];
for(inti=pos;i>0;i--)
wantQ[i]=wantQ[i-1];
wantQ[0]=str;
for(inti=0;i{System.out.println(wantQ[i]);}}publicsynchronizedbooleanhasWriterWant(){return(writerWantCount>0);}publicsynchronizedbooleanhasReaderWant(){return(readerWantCount>0);}publicsynchronizedvoidacquireReadLock(ReaderWriterAppletapplet,intid){readWantQ[readTop]=id;//numsistheindexforreadWantQreadTop=(readTop+1)%rn;readerWantCount++;repaint();notifyAll();enterQueue("R",id);applet.r[id].status=1;//wantinapplet.mc.println(applet.r[id].status,"r",id);try{applet.r[id].sleep(frameDelay);}catch(InterruptedExceptione){}if(applet.writerPriority){while(hasWriterWant()||writeLock){applet.r[id].status=3;applet.mc.println(applet.r[id].status,"r",id);try{wait();}catch(InterruptedExceptione){}}//endofwhileloop}//endofifstatementelseif(applet.readerPriority){while(readWantQ[readBottom]!=id){try{wait();}catch(InterruptedExceptione){}}changePosition("R",id);}else{applet.r[id].status=3;applet.mc.println(applet.r[id].status,"r",id);while(!wantQ[wantBottom].equals("R"+id)){try{wait();}catch(InterruptedExceptione){}}}while(writeLock)//ifthereisanywriteriswriting{applet.r[id].status=3;applet.mc.println(applet.r[id].status,"r",id);try{wait();}catch(InterruptedExceptione){}}if(readLock==false){readLock=true;notifyAll();}readBottom=(readBottom+1)%rn;readerWantCount--;dequeue("R",id);readerCount++;repaint();applet.r[id].status=2;applet.mc.println(applet.r[id].status,"r",id);System.out.println("Reader"+id+"isreading");notifyAll();}publicsynchronizedbooleanhasReader(){return(readerCount>0);}publicsynchronizedvoidreleaseReadLock(ReaderWriterAppletapplet,intid){readerCount--;notifyAll();if(!hasReader()){readLock=false;notifyAll();applet.r[id].status=4;applet.mc.println(applet.r[id].status,"r",id);}repaint();}publicsynchronizedvoidacquireWriteLock(ReaderWriterAppletapplet,intid){writerWantQ[writerTop]=id;writerTop=(writerTop+1)%wn;writerWantCount++;notifyAll();repaint();enterQueue("W",id);applet.w[id].status=1;//wantinapplet.mc.println(applet.w[id].status,"w",id);try{applet.w[id].sleep(frameDelay);}catch(InterruptedExceptione){} if(applet.writerPriority){while(writerWantQ[writerBottom]!=id){try{wait();}catch(InterruptedExceptione){}}changePosition("W",id);while(readLock||writeLock){try{wait();}catch(InterruptedExceptione){}}}elseif(applet.readerPriority){while(!(wantQ[wantBottom].equals("W"+id))||hasReaderWant()||readLock||writeLock){try{wait();}catch(InterruptedExceptione){}}System.out.println("Writer"+id+"moveforward");}else{while(!(wantQ[wantBottom].equals("W"+id))){try{wait();}catch(InterruptedExceptione){}}while(readLock||writeLock){try{wait();}catch(InterruptedExceptione){}}}writeLock=true;System.out.println("Writer"+id+"Gotthelock******");notifyAll();dequeue("W",id);writerBottom=(writerBottom+1)%wn;writerID=id;writerWantCount--;writerCount++;notifyAll();repaint();applet.w[id].status=2;applet.mc.println(applet.w[id].status,"w",id);}publicsynchronizedvoidreleaseWriteLock(ReaderWriterAppletapplet,intid){System.out.println("Writer"+id+"releasedthelock");writerCount--;writerID=0;writeLock=false;notifyAll();repaint();} publicvoidclear(){writerBottom=writerTop=0;readBottom=readTop=0;writerWantCount=0;readerWantCount=0;readerCount=0;writerCount=0;readLock=writeLock=false;writerWantQ=newint[wn];readWantQ=newint[rn];wantQ=newString[wn+rn];wantTop=wantBottom=0;}publicvoidpaint(Graphicsg){intxpos=630;intypos=5;g.setFont(newFont("TimesRoman",Font.BOLD,11));g.setColor(Color.green);g.draw3DRect(xpos,ypos,10,10,true);g.fillRect(xpos,ypos,10,10);g.drawString("Reading",xpos+15,ypos+10);g.setColor(Color.red);g.draw3DRect(xpos,ypos+14,10,10,true);g.fillRect(xpos,ypos+14,10,10);g.drawString("Writing",xpos+15,ypos+25);g.setColor(Color.blue);g.draw3DRect(xpos,ypos+28,10,10,true);g.fillRect(xpos,ypos+28,10,10);g.drawString("Empty",xpos+15,ypos+40);g.setFont(newFont("TimesRoman",Font.BOLD,14));g.setColor(Color.blue);xpos=40;ypos=50;g.drawString("WaitingQueue",xpos-5,ypos-20);inti=wantBottom;for(intj=0;j{if(wantQ[i].equals("W1")||wantQ[i].equals("W2")||wantQ[i].equals("W3")||wantQ[i].equals("W4")||wantQ[i].equals("W5")){g.setColor(Color.red);g.drawString(wantQ[i],xpos+450-30*j,ypos-18);g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);}if(wantQ[i].equals("R1")||wantQ[i].equals("R2")||wantQ[i].equals("R3")||wantQ[i].equals("R4")||wantQ[i].equals("R5")){g.setColor(Color.green);g.drawString(wantQ[i],xpos+450-30*j,ypos-18);g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);}i=(i+1)%(wn+rn);}if(readLock)g.setColor(Color.green);elseif(writeLock)g.setColor(Color.red);elseg.setColor(Color.blue);g.draw3DRect(xpos+250,ypos+20,100,100,true);g.fillRect(xpos+250,ypos+20,100,100);if(readLock){g.setColor(Color.black);g.drawString("Reading",xpos+270,ypos+60);}elseif(writeLock){g.setColor(Color.black);g.drawString("W"+Integer.toString(writerID),xpos+280,ypos+45);g.drawString("Writing",xpos+270,ypos+60);}}}MessageCanvas:importjava.awt.*;classMessageCanvasextendsCanvas{privateFontfont;privateFontMetricsfm;privateint[]writerStatus;privateint[]readerStatus;privateintmsgHeight;privateintmsgWidth;privateintpn,cn;privateintframeDelay=256;publicMessageCanvas(){resize(size().width,50);setBackground(Color.green);font=newFont("TimesRoman",1,18);fm=getFontMetrics(font);msgHeight=fm.getHeight();}publicvoidsetMessage(intwriterN,intreaderN){pn=writerN;cn=readerN;writerStatus=newint[pn+1];readerStatus=newint[cn+1];repaint();}voidprintln(Strings){msgWidth=fm.stringWidth(s);repaint();}voidprintln(ints,Stringst,intid){if(st.equals("w"))writerStatus[id]=s;elsereaderStatus[id]=s;repaint();}voidprintln(ints,intnumber,Stringst,intid){if(st.equals("w")){writerStatus[id]=s;}else{readerStatus[id]=s;}repaint();}publicvoidpaint(Graphicsg){g.setFont(font);intxpos=60;intypos=40;g.drawString("StatusofReaders:
publicsynchronizedbooleanhasWriterWant()
return(writerWantCount>0);
publicsynchronizedbooleanhasReaderWant()
return(readerWantCount>0);
publicsynchronizedvoidacquireReadLock(ReaderWriterAppletapplet,intid)
readWantQ[readTop]=id;//numsistheindexforreadWantQ
readTop=(readTop+1)%rn;
readerWantCount++;
notifyAll();
enterQueue("R",id);
applet.r[id].status=1;//wantin
applet.mc.println(applet.r[id].status,"r",id);
try{applet.r[id].sleep(frameDelay);}catch(InterruptedExceptione){}
if(applet.writerPriority)
while(hasWriterWant()||writeLock)
applet.r[id].status=3;
}//endofwhileloop
}//endofifstatement
elseif(applet.readerPriority)
while(readWantQ[readBottom]!
=id)
changePosition("R",id);
else
wantQ[wantBottom].equals("R"+id))
while(writeLock)//ifthereisanywriteriswriting
if(readLock==false)
readLock=true;
readBottom=(readBottom+1)%rn;
readerWantCount--;
dequeue("R",id);
readerCount++;
applet.r[id].status=2;
System.out.println("Reader"+id+"isreading");
publicsynchronizedbooleanhasReader()
return(readerCount>0);
publicsynchronizedvoidreleaseReadLock(ReaderWriterAppletapplet,intid)
readerCount--;
if(!
hasReader())
applet.r[id].status=4;
publicsynchronizedvoidacquireWriteLock(ReaderWriterAppletapplet,intid)
writerWantQ[writerTop]=id;
writerTop=(writerTop+1)%wn;
writerWantCount++;
enterQueue("W",id);
applet.w[id].status=1;//wantin
applet.mc.println(applet.w[id].status,"w",id);
try{applet.w[id].sleep(frameDelay);}catch(InterruptedExceptione){}
while(writerWantQ[writerBottom]!
changePosition("W",id);
while(readLock||writeLock)
(wantQ[wantBottom].equals("W"+id))||hasReaderWant()||readLock||writeLock)
System.out.println("Writer"+id+"moveforward");
(wantQ[wantBottom].equals("W"+id)))
writeLock=true;
System.out.println("Writer"+id+"Gotthelock******");
dequeue("W",id);
writerBottom=(writerBottom+1)%wn;
writerID=id;
writerWantCount--;
writerCount++;
applet.w[id].status=2;
publicsynchronizedvoidreleaseWriteLock(ReaderWriterAppletapplet,intid)
System.out.println("Writer"+id+"releasedthelock");
writerCount--;
publicvoidclear()
writerBottom=writerTop=0;
readBottom=readTop=0;
writerWantCount=0;
readerWantCount=0;
readerCount=0;
writerCount=0;
readLock=writeLock=false;
wantQ=newString[wn+rn];
publicvoidpaint(Graphicsg){
intxpos=630;
intypos=5;
g.setFont(newFont("TimesRoman",Font.BOLD,11));
g.setColor(Color.green);
g.draw3DRect(xpos,ypos,10,10,true);
g.fillRect(xpos,ypos,10,10);
g.drawString("Reading",xpos+15,ypos+10);
g.setColor(Color.red);
g.draw3DRect(xpos,ypos+14,10,10,true);
g.fillRect(xpos,ypos+14,10,10);
g.drawString("Writing",xpos+15,ypos+25);
g.setColor(Color.blue);
g.draw3DRect(xpos,ypos+28,10,10,true);
g.fillRect(xpos,ypos+28,10,10);
g.drawString("Empty",xpos+15,ypos+40);
g.setFont(newFont("TimesRoman",Font.BOLD,14));
xpos=40;
ypos=50;
g.drawString("WaitingQueue",xpos-5,ypos-20);
inti=wantBottom;
for(intj=0;j{if(wantQ[i].equals("W1")||wantQ[i].equals("W2")||wantQ[i].equals("W3")||wantQ[i].equals("W4")||wantQ[i].equals("W5")){g.setColor(Color.red);g.drawString(wantQ[i],xpos+450-30*j,ypos-18);g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);}if(wantQ[i].equals("R1")||wantQ[i].equals("R2")||wantQ[i].equals("R3")||wantQ[i].equals("R4")||wantQ[i].equals("R5")){g.setColor(Color.green);g.drawString(wantQ[i],xpos+450-30*j,ypos-18);g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);}i=(i+1)%(wn+rn);}if(readLock)g.setColor(Color.green);elseif(writeLock)g.setColor(Color.red);elseg.setColor(Color.blue);g.draw3DRect(xpos+250,ypos+20,100,100,true);g.fillRect(xpos+250,ypos+20,100,100);if(readLock){g.setColor(Color.black);g.drawString("Reading",xpos+270,ypos+60);}elseif(writeLock){g.setColor(Color.black);g.drawString("W"+Integer.toString(writerID),xpos+280,ypos+45);g.drawString("Writing",xpos+270,ypos+60);}}}MessageCanvas:importjava.awt.*;classMessageCanvasextendsCanvas{privateFontfont;privateFontMetricsfm;privateint[]writerStatus;privateint[]readerStatus;privateintmsgHeight;privateintmsgWidth;privateintpn,cn;privateintframeDelay=256;publicMessageCanvas(){resize(size().width,50);setBackground(Color.green);font=newFont("TimesRoman",1,18);fm=getFontMetrics(font);msgHeight=fm.getHeight();}publicvoidsetMessage(intwriterN,intreaderN){pn=writerN;cn=readerN;writerStatus=newint[pn+1];readerStatus=newint[cn+1];repaint();}voidprintln(Strings){msgWidth=fm.stringWidth(s);repaint();}voidprintln(ints,Stringst,intid){if(st.equals("w"))writerStatus[id]=s;elsereaderStatus[id]=s;repaint();}voidprintln(ints,intnumber,Stringst,intid){if(st.equals("w")){writerStatus[id]=s;}else{readerStatus[id]=s;}repaint();}publicvoidpaint(Graphicsg){g.setFont(font);intxpos=60;intypos=40;g.drawString("StatusofReaders:
if(wantQ[i].equals("W1")||wantQ[i].equals("W2")
||wantQ[i].equals("W3")||wantQ[i].equals("W4")
||wantQ[i].equals("W5"))
g.drawString(wantQ[i],xpos+450-30*j,ypos-18);
g.draw3DRect(xpos+445-30*j,ypos-35,28,28,true);
if(wantQ[i].equals("R1")||wantQ[i].equals("R2")
||wantQ[i].equals("R3")||wantQ[i].equals("R4")
||wantQ[i].equals("R5"))
i=(i+1)%(wn+rn);
if(readLock)g.setColor(Color.green);
elseif(writeLock)g.setColor(Color.red);
elseg.setColor(Color.blue);
g.draw3DRect(xpos+250,ypos+20,100,100,true);
g.fillRect(xpos+250,ypos+20,100,100);
if(readLock)
g.setColor(Color.black);
g.drawString("Reading",xpos+270,ypos+60);
elseif(writeLock)
g.drawString("W"+Integer.toString(writerID),xpos+280,ypos+45);
g.drawString("Writing",xpos+270,ypos+60);
MessageCanvas:
classMessageCanvasextendsCanvas
privateint[]writerStatus;
privateint[]readerStatus;
privateintmsgHeight;
privateintmsgWidth;
privateintpn,cn;
privateintframeDelay=256;
publicMessageCanvas()
resize(size().width,50);
setBackground(Color.green);
font=newFont("TimesRoman",1,18);
msgHeight=fm.getHeight();
publicvoidsetMessage(intwriterN,intreaderN)
pn=writerN;
cn=readerN;
writerStatus=newint[pn+1];
readerStatus=newint[cn+1];
voidprintln(Strings)
msgWidth=fm.stringWidth(s);
voidprintln(ints,Stringst,intid)
if(st.equals("w"))
writerStatus[id]=s;
readerStatus[id]=s;
voidprintln(ints,intnumber,Stringst,intid)
publicvoidpaint(Graphicsg)
g.setFont(font);
intxpos=60;
intypos=40;
g.drawString("StatusofReaders:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1