publicclassSyncTest{privateintx;privateinty;publicsynchronizedvoidsetX(inti)(x=1;)publicsynchronizedvoidsetY(inti)(y=1;)publicsynchronizedvoidsetXY(int1)(setX(i);setY(i);)publicsynchronizedBooleancheck()(returnx!=y;)}Underwhichconditionswillcheck()returntruewhencalledfromadifferentclass?A.Check()canneverreturntrue.B.Check()canreturntruewhensetXYiscalledbymultiplethreads.C.Check()canreturntruewhenmultiplethreadscallsetXandsetYseparately.D.Check()canonlyreturntrueifSyncTestischangedtoallowxandytobesetseparately.

publicclassSyncTest{privateintx;privateinty;publicsynchronizedvoidsetX(inti)(x=1;)publicsynchronizedvoidsetY(inti)(y=1;)publicsynchronizedvoidsetXY(int1)(setX(i);setY(i);)publicsynchronizedBooleancheck()(returnx!=y;)}Underwhichconditionswillcheck()returntruewhencalledfromadifferentclass?

A.Check()canneverreturntrue.

B.Check()canreturntruewhensetXYiscalledbymultiplethreads.

C.Check()canreturntruewhenmultiplethreadscallsetXandsetYseparately.

D.Check()canonlyreturntrueifSyncTestischangedtoallowxandytobesetseparately.


相关考题:

( 18 )阅读下列程序Public class Test implements Runnable{Private int x=0;Private int y=o;boolean flag=true;Public static void main(string[ ] args) {Test r =new Test( );Thead t1=new Thead(r);Thead t2=new Thead(r);t1.start( );t2.start( );}Public void run(){While(flag) {x++;y++;system.out.println( “ ( ” +x_ “ , ” +y+ ” ) ” );if (x=10)flag=false;}}}下列对程序运行结果描述的选项中,正确的是A)每行的( x,y )中,可能有;每一对( x,y )值都出现两次。B)每行的( x,y )中,可能有;每一对( x,y )值仅出现一次。C)每行的( x,y )中,可能有 x=y ;每一对( x,y )值都出现两次。D)每行的( x,y )中,可能有 x=y ;每一对( x,y )值都出现一次。

publicclassTestFive{privateintx;publicvoidfoo(){intcurrent=x;x=current+1;}publicvoidgo(){for(inti=0;i5;i++){newThread(){publicvoidrun(){foo();System.out.print(x+,);}}.start();}}}Whichtwochanges,takentogether,wouldguaranteetheoutput:1,2,3,4,5,?()A.Movetheline12printstatementintothefoo()method.B.Changeline7topublicsynchronizedvoidgo(){.C.Changethevariabledeclarationonline3toprivatevolatileintx;.D.Wrapthecodeinsidethefoo()methodwithasynchronized(this)block.E.Wraptheforloopcodeinsidethego()methodwithasynchronizedblocksynchronized(this){//forloopcodehere}.

publicabstractclassShape{privateintx;privateinty;publicabstractvoiddraw();publicvoidsetAnchor(intx,inty){this.x=x;this.y=y;}}WhichtwoclassesusetheShapeclasscorrectly?() A.publicclassCircleimplementsShape{privateintradius;}B.publicabstractclassCircleextendsShape{privateintradius;}C.publicclassCircleextendsShape{privateintradius;publicvoiddraw();}D.publicabstractclassCircleimplementsShape{privateintradius;publicvoiddraw();}E.publicclassCircleextendsShape{privateintradius;publicvoiddraw(){/*codehere*/}}F.publicabstractclassCircleimplementsShape{privateintradius;publicvoiddraw(){/codehere*/}}

1.publicclassXimplementsRunnable(2.privateintx;3.privateinty;4.5.publicstaticvoidmain(String[]args)6.Xthat=newX();7.(newThread(that)).start();8.(newThread(that)).start();9.)10.11.publicvoidrun()(12.for(;;)(13.x++;14.y++;15.System.out.printIn(x=”+x+,y=”+y);16.)17.)Whatistheresult?()A.Errorsatlines7and8causecompilationtofail.B.Theprogramprintspairsofvaluesforxandythatmightnotalwaysbethesameonthesameline(forexample,“x=2,y=1”).C.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”.Inaddition,eachvalueappearstwice(forexample,“x=1,y=1”followedby“x=1,y=1”).D.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”.Inaddition,eachvalueappearsonlyforonce(forexample,“x=1,y=1”followedby“x=2,y=2”).

publicclassXimplementsRunnable(privateintx;privateinty;publicstaticvoidmain(String[]args)(Xthat=newX();(newThread(that)).start();(newThread(that)).start();)publicsynchronizedvoidrun()(for(;;)(x++;y++;System.out.printIn(x=+x+,y=+y);)))Whatistheresult?()A.Anerroratline11causescompilationtofail.B.Errorsatlines7and8causecompilationtofail.C.Theprogramprintspairsofvaluesforxandythatmightnotalwaysbethesameonthesameline(forexample,“x=2,y=1”)D.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”.Inaddition,eachvalueappearstwice(forexample,“x=1,y=1”followedby“x=1,y=1”)E.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”.Inaddition,eachvalueappearstwice(forexample,“x=1,y=1”followedby“x=2s,y=2”)

publicclassSyncTest(privateintx;privateinty;privatesynchronizedvoidsetX(inti)(x=1;)privatesynchronizedvoidsetY(inti)(y=1;)publicvoidsetXY(int1)(setX(i);setY(i);)publicsynchronizedBooleancheck()(returnx!=y;))Underwhichconditionswillcheck()returntruewhencalledfromadifferentclass?A.Check()canneverreturntrue.B.Check()canreturntruewhensetXYiscalledbymultiplethreads.C.Check()canreturntruewhenmultiplethreadscallsetXandsetYseparately.D.Check()canonlyreturntrueifSyncTestischangedtoallowxandytobesetseparately.

publicclassXimplementsRunnable{privateintx;privateinty;publicstaticvoidmain(String[]args){Xthat=newX();(newThread(that)).start();(newThread(that)).start();}publicvoidrun(){for(;;){synchronized(this){x++;y++;}System.out.println(Thread.currentThread().getName()+x=+x+,y=+y);}}}Whatistheresult?()A.Compilationfails.B.Theprogramprintspairsofvaluesforxandythatmightnotalwaysbethesameonthesameline(forexample,“x=2,y=1”).C.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”). Inaddition,eachvalueappearsonlyonce(forexample,“x=1,y=1”followedby“x=2,y=2”). Thethreadnameatthestartofthelineshowsthatboththreadsareexecutingconcurrently.D.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”). Inaddition,eachvalueappearsonlyonce(forexample,“x=1,y=1”followedby“x=2,y=2”). Thethreadnameatthestartofthelineshowsthatonlyasinglethreadisactuallyexecuting.

publicclassSyncTest{privateintx;privateinty;privatesynchronizedvoidsetX(inti){x=i;}privatesynchronizedvoidsetY(inti){y=i;}publicvoidsetXY(inti){setX(i);setY(i);}publicsynchronizedbooleancheck(){returnx!=y;}}Underwhichconditionwillcheckreturntruewhencalledfromadifferentclass?()A.checkcanneverreturntrue.B.checkcanreturntruewhensetXYiscalledbymultiplethreads.C.checkcanreturntruewhenmultiplethreadscallsetXandsetYseparately.D.checkcanreturntrueonlyifSyncTestischangedtoallowxandytobesetseparately.

publicclassXimplementsRunnable(privateintx;privateinty;publicstaticvoidmain(Stringargs)(Xthat=newX();(newThread(that)).start();(newThread(that)).start();)publicsynchronizedvoidrun()(for(;;)(x++;y++;System.out.printIn(x=+x+,y=+y);)))Whatistheresult?()A.Anerroratline11causescompilationtofail.B.Errorsatlines7and8causecompilationtofail.C.Theprogramprintspairsofvaluesforxandythatmightnotalwaysbethesameonthesameline(forexample,“x=2,y=1”)D.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”.Inaddition,eachvalueappearstwice(forexample,“x=1,y=1”followedby“x=1,y=1”)E.Theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesameline(forexample,“x=1,y=1”.Inaddition,eachvalueappearstwice(forexample,“x=1,y=1”followedby“x=2s,y=2”)