题目内容
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?()
查看答案
搜索结果不匹配?点我反馈
更多问题