如何暂停并稍后继续一个线程的执行

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

如何暂停并稍后继续一个线程的执行?  
  比如:一个从1到10000的累加程序,我想让程序运行100ms时,暂停执行;  
  1100m后继续执行。  
  给思路的,介绍书目的都有分~  
  呵呵,给出解决代码的,那就不用说了:p  
 

· 网友精彩回答:

发表者:smlovetp

class   threadtest{  
          public   static   void   main(string[]   args){  
                    long   start   =   system.currenttimemillis();     //得到当前的毫秒时间  
                    for   (int   i   =   1;i<=   10000;i++){  
                            if   (system.currenttimemillis()   -   start   ==   100){  
                                    try{  
                                            thread.sleep(1100);  
                                    }catch(exception   e){  
                                            e.printstacktrace();        
                                            system.exit(0);  
                                    }  
                            }  
                            system.out.println(i);  
                    }  
          }  
  }

发表者:believefym

知道为什么吗?  
   
  因为程序运行时间不是像你认为的每个毫秒都会到你的if里面判断  
  事实上是system.currenttimemillis()   -   start   ==   100几乎不可能  
   
  比如说,当i=50时,system.currenttimemillis()   -   start   =99,  
  而当i=51时,这个值是101,这个100是到不了的

发表者:mooninsun

假如线程t1在下载  
  那么button   pause   addactionlistener完成t1.wait();  
  在button   resume   的addactionlistener完成t1.notify();

发表者:mooninsun

改用wait()和notify(),功能没变  
  import   javax.swing.*;  
  import   java.awt.*;  
  import   java.awt.event.*;  
   
  class   ttext  
          extends   thread   {  
      private   jtextfield   t   =   new   jtextfield(15);  
      private   jbutton   bp   =   new   jbutton("pause");  
      private   int   count   =   0;  
      private   boolean   startf   =   false,   pus   =   false;  
      public   ttext(container   c)   {  
          c.add(t);  
          bp.addactionlistener(new   actionlistener()   {  
              public   void   actionperformed(actionevent   e)   {  
                  pus   =   true;  
              }  
          });  
          c.add(bp);  
          new   tnotify(c,   this);  
          start();  
      }  
   
      public   void   startt()   {  
          startf   =   true;  
      }  
   
      public   synchronized   void   run()   {     //注意synchronized,不然会产生  
                                                                          //异常illegalmonitorstateexception  
          while   (true)   {  
              if   (startf)   {  
                  t.settext(integer.tostring(count++));  
              }  
              try   {  
                  if(pus)   {  
                      wait();                         //改用wait();  
                      pus   =   !pus;  
                  }  
                  sleep(100);  
              }  
              catch   (interruptedexception   e)   {  
                  system.err.println("interrupted");  
              }  
          }  
      }  
  }  
   
  class   tnotify  
          extends   thread   {  
      private   ttext   t;  
      private   boolean   startf;  
      private   jbutton   br   =   new   jbutton("resume");  
      public   tnotify(container   c,   ttext   t)   {  
          this.t   =   t;  
          br.addactionlistener(new   actionlistener()   {  
              public   void   actionperformed(actionevent   e)   {  
                  startt();  
              }  
          });  
          c.add(br);  
          start();  
      }  
      public   void   startt()   {  
          startf   =   true;  
      }  
      public   void   run()   {  
          while   (true)   {  
              if   (startf)   {  
                  synchronized(t)   {                       //注意synchronized,不然会产生  
                                                                          //异常illegalmonitorstateexception  
                      t.notify();  
                  }  
                  startf   =   !startf;  
              }  
              try   {  
                  sleep(100);  
              }  
              catch   (interruptedexception   e)   {  
                  system.err.println("interrupted");  
              }  
          }  
      }  
  }  
   
  public   class   threadt  
          extends   jframe   {  
      private   jbutton  
              bs   =   new   jbutton("start");  
      private   jpanel   p   =   new   jpanel();  
      private   ttext   t;  
      private   tnotify   tn;  
      public   threadt()   throws   headlessexception   {  
          super("test");  
          this.setsize(300,   200);  
          container   cp   =   getcontentpane();  
          cp.setlayout(new   flowlayout());  
          t   =   new   ttext(cp);  
          bs.addactionlistener(new   actionlistener()   {  
              public   void   actionperformed(actionevent   e)   {  
                  t.startt();  
              }  
          });  
          cp.add(bs);  
          this.setvisible(true);  
          this.setdefaultcloseoperation(jframe.exit_on_close);  
   
      }  
   
      public   static   void   main(string[]   args)   throws   headlessexception   {  
          threadt   threadt1   =   new   threadt();  
      }  
  }

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved