importjava.util.*;
publicclassNameList{
privateListnames=newArrayList();
publicsynchronizedvoidadd(Stringname){names.add(name);}
publicsynchronizedvoidprintAll(){
for(inti=0;iSystem.out.print(names.get(i)+);
}
}
publicstaticvoidmain(String[]args){
finalNameListsl=newNameList();
for(inti=0;i<2;i++){
newThread(){
publicvoidruin(){
sl.add(”A”);
sl.add(”B”);
sl.add(”C”);
sl.printAll();
}
}.start();
}
}
}
Whichtwostatementsaretrueifthisclassiscompiledandrun?()
第1题
or(inti=0;i<5;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}.
第2题
A.privatesynchronizedObjecto;
B.voidgo(){synchronized(){/*codehere*/}}
C.publicsynchronizedvoidgo(){/*codehere*/}
D.privatesynchronized(this)voidgo(){/*codehere*/}
E.voidgo(){synchronized(Object.class){/*codehere*/}}
F.voidgo(){Objecto=newObject();synchronized(o){/*codehere*/}}
第3题
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Synchronizingtherun()methodwouldmaketheclassthread-safe.
D.Thedatainvariable“x”areprotectedfromconcurrentaccessproblems.
E.DeclaringthedoThings()methodasstaticwouldmaketheclassthread-safe.
F.WrappingthestatementswithindoThings()inasynchronized(newObject()){}blockwouldmaketheclassthread-safe.
第4题
publicclassTransfers{
publicstaticvoidmain(String[]args)throwsException{
Recordr1=newRecord();
Recordr2=newRecord();
doTransfer(r1,r2,5);
doTransfer(r2,r1,2);
doTransfer(r1,r2,1);
//printtheresult
System.out.println(”rl=+r1.get()+,r2=”+r2.get());
}
privatestaticvoiddoTransfer(
finalRecorda,finalRecordb,finalintamount){
Threadt=newThread(){
publicvoidrun(){
newClerk().transfer(a,b,amount);
}
};
t.start();
}
}
classClerk{
publicsynchronizedvoidtransfer(Recorda,Recordb,intamount){
synchronized(a){
synchronized(b){
a.add(-amount);
b.add(amount);
}
}
}
}
classRecord{
intnum=10;
publicintget(){returnnum;}
publicvoidadd(intn){num=num+n;}
}
IfTransfers.main()isrun,whichthreearetrue?()
第5题
classComputationextendsThread{
privateintnum;
privatebooleanisComplete;
privateintresult;
publicComputation(intnum){this.num=num;}
publicsynchronizedvoidrun(){
result=num*2;
isComplete=true;
notify();
}
publicsynchronizedintgetResult(){
while(!isComplete){
try{
wait();
}catch(InterruptedExceptione){}
}
returnresult;
}
publicstaticvoidmain(String[]args){
Computation[]computations=newComputation[4];
for(inti=0;i<computations.length;i++){
computations[i]=newComputation(i);
computations[i].start();
}
for(Computationc:computations)
System.out.print(c.getResult()+);
}
}
Whatistheresult?()
第6题
A.ThiscodemaythrowanInterruptedException.
B.ThiscodemaythrowanIllegalStateException.
C.ThiscodemaythrowaTimeoutExceptionaftertenminutes.
D.Thiscodewillnotcompileunless“obj.wait()”isreplacedwith“((Thread)obj).wait()”.
E.Reversingtheorderofobj.wait()andobj.notify()maycausethismethodtocompletenormally.
F.Acalltonotify()ornotifyAll()fromanotherthreadmaycausethismethodtocompletenormally.
第7题
.Thethreadfooiscurrentlyexecutingbar.wait().Fromanotherthread,whichstatementisthemostreliablewaytoensuethatfoowillstopexecutingwait()?
A.foo.notify();
B.bar.notify();
C.foo.notifyAll();
D.Thread.notify();
E.bar.notiFYAll();
F.Object.notify();
第8题
A.anyclass
B.onlytheTargetclass
C.anyclassinthetestpackage
D.anyclassthatextendsTarget
第9题
1.publicclassTarget{2.privateinti=0;3.publicintaddOne(){4.return++i;5.}6.}And:1.publicclassClient{2.publicstaticvoidmain(String[]args){3.System.out.println(newTarget().addOne());4.}5.}WhichchangecanyoumaketoTargetwithoutaffectingClient?()
A.Line4ofclassTargetcanbechangedtoreturni++;
B.Line2ofclassTargetcanbechangedtoprivateinti=1;
C.Line3ofclassTargetcanbechangedtoprivateintaddOne(){
D.Line2ofclassTargetcanbechangedtoprivateIntegeri=0;
第10题
A.Itcanbeanyclass.
B.Noclasshasaccesstobase.
C.Theclassmustbelongtothegeometrypackage.
D.TheclassmustbeasubclassoftheclassHypotenuse.
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!