实例要求:
设计一个线程操作类,要求可以产生3个线程对象,并可以分别设置3个线程的休眠时间,那么此类该如何设计?
1.使用Thread类完成
class MyThread extends Thread {
    
   private int time;
   public MyThread( String    name, int time) {
     super(name);
     this.time = time;
    
  }

  @Override
   public void run() {
     try {
      Thread.sleep( this.time);
    } catch (InterruptedException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + "线程休眠"
        + this.time + "毫秒");
  }

}
public class ThreadDemo {

   /**
    * 多次运行,观察运行结果
    */

   public static void main(String[] args) {
    
    MyThread t1 = new MyThread( "线程A",2000);
    MyThread t2 = new MyThread( "线程B",4000);
    MyThread t3 = new MyThread( "线程C",1000);
    t1.start();
    t2.start();
    t3.start();
  }

}
2.实现Runnable接口完成
class MyThread implements Runnable{

private String name;
private int time;
   public MyThread(String name, int time) {
   super();
   this.name = name;
   this.time = time;
}

  @Override
   public void run() {
     // TODO Auto-generated method stub
     try {
      Thread.sleep( this.time);
    } catch (InterruptedException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println( this.name + "休眠的毫秒是" + this.time);
  }
    
    

}
public class ThreadDemo {

   /**
    * 多次运行,观察运行结果
    */

   public static void main(String[] args) {
    MyThread t1 = new MyThread( "线程A", 1000);
    MyThread t2 = new MyThread( "线程B", 4000);
    MyThread t3 = new MyThread( "线程C", 2000);
     new Thread(t1).start();
     new Thread(t2).start();
     new Thread(t3).start();
  }

}