当前页面: 开发资料首页 → Java 专题 → 中断一个正在休眠的线程 
中断一个正在休眠的线程 
摘要: 中断一个正在休眠的线程  
                     </td>
              </tr>
              <tr>
                <td height="35" valign="top" class="ArticleTeitle">   当一个线程运行时,另一个线程可以调用Thread对象的interrupt()方法来中断它: 
                      public void interrupt() 
                      这个方法只是在目标线程中将中断标志设为true(它是Thread的内部标志),没有其他影响,表示它已经被中断,并立即返回。如果一个处于休眠的线程被中断,将导致它抛出InterruptedException。 
                  public class SleepInterrupt extends Object implements Runnable { 
                  public void run() {//计算1+2+3+...+200 
                                  int s=0; 
                                  int i=0; 
                                 try { 
                                      System.out.println("in run() - about to sleep for 20 seconds"); 
                                      for(i=1;i<200;i++){ 
                                          s=s+i; 
                                          Thread.sleep(20000);//进入休眠 
                                               System.out.println("in run() - woke up"); 
                                        } 
                                 } catch ( InterruptedException x ) { 
                                          System.out.println("in run() - interrupted while sleeping"); 
                                          System.out.println("i="+i+"; s="+s);//线程被中断后,任务执行情况。 
                                        //  return; 
                                 } 
                                  System.out.println("i="+i+"; s="+s);//如果被中断,任务将不能完成。 
                                  System.out.println("in run() - doing stuff after nap"); 
                                  System.out.println("in run() - leaving normally"); 
                           } 
                  
                  
                         public static void main(String[] args) { 
                                  SleepInterrupt si = new SleepInterrupt(); 
                                  Thread t = new Thread(si);//构造线程 
                                  t.start(); 
                                  // 确保新线程有机会运行一段时间 
                                  try { Thread.sleep(2000); }  
                                  catch ( InterruptedException x ) { } 
                                  System.out.println("in main() - interrupting other thread"); 
                                  t.interrupt();//在主线程中中断另一个正在休眠的线程。 
                                  System.out.println("in main() - leaving"); 
                         } 
                  } 
                  
               
                  <table width="679" height="28" border="0">
                    <tr>
                      <td width="304">运行结果: 
                          
C:\java>javac    SleepInterrupt.java 
C:\java>java     SleepInterrupt 
in run() - about to sleep for 20 seconds 
in main() - interrupting other thread 
in run() - interrupted while sleeping 
i=1; s=1 
i=1; s=1 
in run() - doing stuff after nap 
in run() - leaving normally 
in main() - leaving
                       
                          
                        
                       
                       
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
                       
                       
                       
                      </td>
                      <td width="365">
</td>
                    </tr>
                  </table>
                                    
                </td>
              </tr>
              <tr>
                
                    
 
↑返回目录 
前一篇: 打印日期的三种方法  
 
后一篇: JSP中的COOKIE操作