可学答题网 > 问答 > SCJP程序员认证考试题库
目录: 标题| 题干| 答案| 搜索| 相关
问题

public class Transfers&ensp


public class Transfers {  public static void main(String[] args) throws Exception {  Record r1 = new Record();  Record r2 = new Record();  doTransfer(r1, r2, 5);  doTransfer(r2, r1, 2);  doTransfer(r1, r2, 1);  // print the result  System.out.println(”rl = “ + r1.get() +“, r2=” + r2.get());  }  private static void doTransfer(  final Record a, final Record b, final int amount) {  Thread t = new Thread() {  public void run() {  new Clerk().transfer(a, b, amount);  }  };  t.start();  }  }  class Clerk {  public synchronized void transfer(Record a, Record b, int amount){  synchronized (a) {  synchronized (b) {  a.add(-amount);  b.add(amount);  }  }  }  }  class Record {  int num=10;  public int get() { return num; }  public void add(int n) { num = num + n; }  }  If Transfers.main() is run, which three are true?()

  • A The output may be “r1 = 6, r2 = 14”.
  • B The output may be “r1 = 5, r2 = 15”.
  • C The output may be “r1 = 8, r2 = 12”.
  • D The code may run (and complete) with no output.
  • E The code may deadlock (without completing) with no output.
  • F M IllegalStateException or InterruptedException may be thrown at runtime.
参考答案
参考解析:
分类:SCJP程序员认证考试题库
相关推荐

1、10. public class Class

10. public class ClassA { 11. public void methodA() { 12. ClassB classB = new ClassB()...

2、public class X { 

public class X { public static void main(String [] args) { try { badMethod(); System.out.p...

3、10. public class Class

10. public class ClassA { 11. public void count(int i) { 12. count(++i); 13. } 14.&en...

4、public class X { 

public class X { public static void main (String[] args)  { byte b = 127; byte&ens...

5、 public class X {

 public class X {  public static void main (String[]args)   {  string s =&ensp...

6、public class X { 

public class X { public static void main (String[]args)  { String s1 = new String ...