1. public class Threads4 {
2. public static void main (String[] args) {
3. new Threads4().go();
4. }
5. public void go() {
6. Runnable r = new Runnable() {
7. public void run() {
8. System.out.print(”foo”);
9. }
10. };
11. Thread t = new Thread(r);
12. t.start();
13. t.start();
14. }
15. }
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编译了一下~抛出了IllegalThreadStateException~~这是为什么呢?
   谁帮我讲解一下~~谢谢~~

解决方案 »

  1.   

      public class Threads5 {
     public static void main (String[] args) {
     Runnable r = new Runnable() {
     public void run() {
     System.out.print("Cat");
     }
     };
      Thread t=new Thread(r) {//这里如果改为Thread t=new Thread()输出结果是一样的,那Thread()括号里的参数用来起什么作用的呢??
       public void run() {
     System.out.print("Dog");
     }
    };
     t.start();
     }
     }
      

  2.   

    有两个 t.start() ,去掉其中一个,程序就正常了,输出 foo
      

  3.   

    LS说的对
    这条语句是让线程t开始 既然开始了看到就不能再开始了Thread t=new Thread(r)
    传递Runnable方法 r是Runnable方法的实例对象
      

  4.   

    start()是让线程t开始 既然开始了看到就不能再开始了