2014-02-17 85 views
2
class test 
{ 
    public static void main(String[] args) { 


     Thread th=new Thread() 
     { 

      public void run(){ 

       for (int i = 1; i <= 18; i++) { 
        System.out.println("run:"+i); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
        } 
       } 

      }}; 
      Thread y=new Thread() 
      { 
       public void run() { 
        for (int i = 1; i < 10; i++) { 
         System.out.println("stop:"+i); 
         try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
         } 
        } 

       } 

      }; 

      th.start(); 
      y.start(); 
      for (int i = 0; i < 5; i++) { 
       System.out.println("main:"+i); 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 

       } 

      } 
      System.out.println("main completed"); 
    } 

} 

這裏我想在線程y完成執行後立即停止線程,我該怎麼做?如何在其他完成後停止一個線程?

+0

您可以讓一個線程中斷另一個線程。 –

回答

0

您可以停止主線程的主要處理,直到線程y完成,然後停止線程。 使用以下:

y.join(); 
th.kill(); \\ Create a custom kill method which stops the thread. Check code. 

您還可以看看CountDownLatch可以用來解決這種情況。 使用下面的代碼:

class test 
{ 
    public static void main(String[] args) { 

     boolean run_th = true; 
     Thread th=new Thread() 
     { 

      public void run(){ 
       while(run_th) { 
        for (int i = 1; i <= 18; i++) { 
         System.out.println("run:"+i); 
         try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
         } 
        } 
       }  
      } 
      public void kill() { 
       run_th = false; 
      } 

     }; 
      Thread y=new Thread() 
      { 
       public void run() { 
        for (int i = 1; i < 10; i++) { 
         System.out.println("stop:"+i); 
         try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
         } 
        } 

       } 

      }; 

      th.start(); 
      y.start(); 
      for (int i = 0; i < 5; i++) { 
       System.out.println("main:"+i); 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 

       } 
      } 
      y.join(); 
      th.kill(); 
      System.out.println("main completed"); 
    } 
} 
+1

不要使用停止方法,它已棄用。 – Bax

+0

...有一個很好的理由。你不想停止一個線程,你想(正常)停止線程正在執行的活動,並且由於線程本身會停止。 – Gimby

+0

是的。我已經編寫了相應的代碼 – Bhoot

0

在原則層面,用一個標誌,完成線程Y設置之前,和線程TH檢查它的每一次。當線程Y將其設置爲true或false並且線程TH讀取該更改時,則可以停止它。

+0

在我的示例中更改以顯示請求 – user3239652

1

您可以使用揮發性布爾變量並將其設置爲。線程y完成後true。在條件檢查停止所需的線程。

0

試試這個,

class test 
{ 
    public static void main(String[] args) { 


     final Thread th=new Thread() 
     { 

      public void run(){ 

       for (int i = 1; i <= 10; i++) { 
        System.out.println("run:"+i); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
        } 
       } 

      }}; 
      Thread y=new Thread() 
      { 
       public void run() { 
        for (int i = 1; i < 5; i++) { 
         System.out.println("stop:"+i); 
         try { 
          Thread.sleep(500); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
         } 
        } 
        th.stop(); 
       } 

      }; 

      th.start(); 
      y.start(); 
      for (int i = 0; i < 2; i++) { 
       System.out.println("main:"+i); 
       try { 
        Thread.sleep(500); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 

       } 

      } 
      System.out.println("main completed"); 
    } 

} 
0
  Thread th=new Thread() 
       { 

       public void run(){ 

      for (int i = 1; i <= 18; i++) { 
          if(y.isAlive()){ 
           System.out.println("run:"+i); 
           try { 
            Thread.sleep(1000); 
           } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
           } 

      }else{ 
     this.stop(); 
} 
    } 


        }}; 
0

您可以使用volatile變量,並用它在執行的線程之間建立依賴關係:

package com.test; 

class ThreadDependency { 
    volatile static boolean isYCompleted = false; 
    public static void main(String[] args) { 
     Thread th = new Thread() { 
      public void run() { 
       for (int i = 1; i <= 18; i++) { 
        if(!isYCompleted) { 
         System.out.println("run:" + i); 
         try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) {} 
        } else { 
         break; 
        } 
       } 
      } 
     }; 

     Thread y = new Thread() { 
      public void run() { 
       for (int i = 1; i < 10; i++) { 
        System.out.println("stop:" + i); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
        } 
        if(i == 9) { 
         isYCompleted = true; 
        } 
       } 
      } 
     }; 

     th.start(); 
     y.start(); 

     for (int i = 0; i < 5; i++) { 
      System.out.println("main:" + i); 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) {} 
     } 
     System.out.println("Main completed"); 
    } 
} 
0

在這裏,我想停止線程只要線程y完成其執行 ,我該怎麼做?

Java爲您提供了所需的方法。所有你需要做的就是填寫你的「TODO」塊。

從JDK文檔引用的interrupt方法:

中斷該線程。

... [剪斷]

如果該線程被阻塞在等待(的調用),等待(長), 或等待(長,INT)對象類的方法,或的加入(), 這個類的方法 join(long),join(long,int),sleep(long)或sleep(long,int),那麼它的中斷狀態將被清除,並且它會 收到一個InterruptedException 。

因此,螺紋y可以調用線程th上的中斷方法並使其收到InterruptedException。這正是爲什麼這些支票在sleep()電話中的原因。現在,只需填寫他們,你就會有你需要的東西:

class test 
{ 
    public static void main(String[] args) { 
     Thread th=new Thread() 
     { 
      public void run(){ 
       for (int i = 1; i <= 18; i++) { 
        System.out.println("run:"+i); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 

         // Here is one way that you can immediately exit th 
         System.out.println("Thread th was interrupted."); 
         System.out.println("y must have finished"); 
         break; 
         // This will immediately exit the loop 
         // and the run method 

        } 
       } 

      }}; 
      Thread y=new Thread() 
      { 
       public void run() { 
        for (int i = 1; i < 10; i++) { 
         System.out.println("stop:"+i); 
         try { 
          Thread.sleep(1000); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
         } 
        } 

        // Here is where we trigger the end of thread th 
        System.out.println("y is finished. Interrupting th."); 
        th.interrupt(); 
        // th will receive the InterruptedException 
       } 
      }; 

      th.start(); 
      y.start(); 
      for (int i = 0; i < 5; i++) { 
       System.out.println("main:"+i); 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
       } 
      } 
      System.out.println("main completed"); 
    } 
} 

故事的寓意是:看看那些自動生成catch塊 - 也許這就是你應該開始。

相關問題