今天看了些线程的scjp题 好混乱觉得 希望高手能帮解释下答案1
 Runnable r = new Runnable() {
 public void run() {
System.out.print("Cat");
}
};
 Threadt=new Thread(r) {
 public void run() {
 System.out.print("Dog");
 }
 };
 t.start();
What is the result?
Copyright Tarena Corporation,2008.All rights reserved
A. Cat
B. Dog
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: B
Question 3
Given:
 public class Threads3 implements Runnable {
 public void run() {
 System.out.print("running");
 }
public static void main(String[] args) {
Thread t = new Thread(new Threads3());
t.run();
 t.run();
t.start();
}
 }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "running".
D. The code executes and prints "runningrunning".
E. The code executes and prints "runningrunningrunning".
Answer: E
Question 4
Click the Exhibit button:
 public class Threads 1 {
 Int x=0;
 public class Runner implements Runnable {
 public void run() {
 int current = 0;
 for(int i=0;i<4;i++){
 current = x;
 System.out.print(current + ", ");
 x = current + 2;
 }
 }
 } public static void main(String[] args) {
Copyright Tarena Corporation,2008.All rights reserved
 new Threads1().go();
 } public void go() {
Runnable r1 = new Runner();
 new Thread(r1).start();
 new Thread(r1 ).start();
 }
 }
Which two are possible results? (Choose two.)
A. 0, 2, 4, 4, 6, 8, 10, 6,
B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
Answer: AC

解决方案 »

  1.   

    最后一问变态啊!
    排除法,两个线程一共打印8次,故d,e错,现在3选2答案A,其中一个线程打印先执行3次循环,打印出0,2,4,在第三次执行完System.out.print(current + ", ");后,执行时间到(此时局部变量current=4,x=4)
     第二个线程来连续执行4次就打印出4,6,8,10。
    再第一个线程执行,因为在一个线程中current=4,执行完x=current+2后x=6.所以最后打印出6.答案C。很简单,两个线程循环轮流执行就是这个答案其它的题很简单,看下Thread类源码,您就什么都明白了。或者是等楼下的答案吧
      

  2.   

    第三题:http://www.javaedu.com.cn/tech/tech0023.htm
      

  3.   


    class Threads4 {
    public static void main(String[] args) {
    new Threads4().go();
    } public void go() {
    Runnable r = new Runnable() {
    public void run() {
    System.out.print("foo");
    }
    };
    Thread t = new Thread(r);
    t.start();
    t.start();
    }
    }What is the result?
    A. Compilation fails.
    B. An exception is thrown at runtime.
    C. The code executes normally and prints "foo".
    D. The code executes normally, but nothing is printed.
    Answer: B
      

  4.   

    Thread类的start方法不能多次调用,否则会抛出一个IllegalThreadStateException异常。
      

  5.   


    import java.util.*; class NameList {
    private List names = new ArrayList(); public synchronized void add(String name) {
    names.add(name);
    } public synchronized void printAll() {
    for (int i = 0; i < names.size(); i++) {
    System.out.print(names.get(i) + " ");
    }
    } public static void main(String[] args) {
    final NameList sl = new NameList();
    for (int i = 0; i < 2; i++) {
    new Thread() {
    public void run() {
    sl.add("A");
    sl.add("B");
    sl.add("C");
    sl.printAll();
    }
    }.start();
    }
    }
    }
    Which two statements are true if this class is compiled and run?
    (Choose two.)
    A. An exception may be thrown at runtime.
    B. The code may run with no output, without exiting.
    C. The code may run with no output, exiting normally.
    D. The code may rum with output "A B A B C C ", then exit.
    E. The code may rum with output "A B C A B C A B C ", then exit.
    F. The code may ruin with output "A A A B C A B C C ", then exit.
    G. The code may ruin with output "A B C A A B C A B C ", then exit.
    Answer: EG
     why?
      

  6.   


     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? (Choose three.)
    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.
    Answer: ABE