sleep(int)
把线程停止一段时间
参数用毫秒来计算
比如
try{
  Thread.currentThread().sleep( 1000 );
}catch( InterruptedException ie ){
  // deal with exception
}
这样就是让当前的线程暂停一秒钟
记得用try和catch

解决方案 »

  1.   

    sleep()是线程自己的动作,wait()是其他对象针对线程做的动作。sleep()在线程中可以比较随意的调用,而wait()必须由一个被两个或两个以上的线程互锁的对象调用。一般书籍对于wait()的用法讲得不好,最好找e文的看看。以下是简单例子。class Test{
        static String s="11111";           //定义字符串对象s
        public static void a(){
             synchronized(s){
                   try{
                         s.wait();       //等待其他线程释放对象s的使用权
                   }catch(java.lang.InterruptedException e){}    
                                  //捕获“中断异常”
                   System.out.println(s.length());
             }
        }
        public static void b(){
             synchronized(s){
                   Thread.sleep(1000);     //让当前线程睡眠一秒
          System.out.println(i);  //输出睡眠时间
                   s.notify();             //通知另外一个线程
             }
        }
        public static void main(String []args){
    //建立新的线程,这里使用匿名内部类
             Thread thread1=new Thread(){
        //重载父类的函数run
                 public void run(){
                      while(true){
                           a();    //调用外部类的函数a()
                      }
                 }
            };
            //同上,建立另外一个线程
            Thread thread2=new Thread(){
        //重载父类寒数run
                 public void run(){
                      while(true){
                           b();    //调用外部类的函数b()
                      }
                 }
            };                             
            thread1.start();
            thread2.start();
        }
    }
      

  2.   

    用sleep(),而wait()需用其他线程用notify()唤醒!!!