面试题:运行下列程序,  会产生什么结果(   )public class X extends Thread implements Runnable{
  public void run(){
    System.out.println("this is run()");
 }
  public static void main(String[] args){
    Thread t=new Thread(new X());
    t.start();
  }
}A.第一行会产生编译错误
B.第六行会产生编译错误
C.第六行会产生运行错误
D.程序会运行和启动创建线程不是有两种方法:
(1)实现Runnable接口
(2)继承Thread类这么这种方法也行? 

解决方案 »

  1.   

    D.程序会运行和启动 public   class   X   extends   Thread   implements   Runnable{ 
        public   void   run(){ 
            System.out.println( "this   is   run() "); 
      } 
        public   static   void   main(String[]   args){ 
            Thread   t=new   Thread(new   X()); //这种写法是把X当作Runnable类型来处理的         /**
             *  也可以这样写
             */
             Thread  tt = new X()                //这种写法是把X当作Thread类型来处理        t.start(); 
            tt.start();
        } 
      

  2.   

    public class X extends Thread{// implements Runnable { // Thread 已经实现了Runnable, 此处多余,无实际意义.
    public void run() {
    System.out.println("this   is   run() ");
    } public static void main(String[] args) {
    Thread t = new Thread(new X()); // 这句是核心, 构造一个线程, new X() 中的 X只要实现Runnable接口即可, 并将在t的调用X.run()方法.
    t.start();
    }
    }
    // 其中
    Thread t = new Thread(new X()); 
    这句是核心, 构造一个线程, new X() 中的 X只要实现Runnable接口即可, 并将在t的start方法中,直接调用X.run()方法.
    实际上, 简单理解就是t的实际运行对象是new X(),而X只需实现Runnable接口.So. 上述代码可以正确运行, 结果就是
    this   is   run() 
      

  3.   

    同志们,
      Thread 是怎么搞的
      1 搞一个类implements Runnable
      2 new 出这个类
      3 new 出thread(将这个类放入到这里)
      4 new 出thread(将这个类放入到这里).start()方法
      搞Thread   只要晓的这4个,什么都晓的了
      

  4.   

    public class X extends Thread implements Runnable{ 
    public void run(){ 
    System.out.println("this is run()"); 

    public static void main(String[] args){ 
    Thread t=new Thread(new X()); 
    t.start(); 

    } 在我这里编译运行都没有问题(是不是你的包没写对)创建线程不是有两种方法: 
    (1)实现Runnable接口 
    (2)继承Thread类 只要用其中一种就可以了,
    public class X extends Thread implements Runnable{ 
    像你这样Thread就被重载了.