2015-09-02 67 views
1

我在下面的代碼中有我的線程框架。 我已經使用了一個簡單的倒計時閂鎖。 我陷入了線程1取決於線程2完成的情況。如果沒有異常,則代碼正常運行。 但是例外的可能性更大。 如果線程2或任何線程發生異常,我希望所有線程停止執行。即使在異常之後,我的獨立線程也會繼續執行。我還在線程1中使用了一個標誌來檢查線程2中是否發生了異常。我有意將除以零異常作爲示例來測試exeption。 我無法找到解決方案。 請幫幫我..!即使在java中出現異常之後線程仍然繼續執行

import java.util.concurrent.CountDownLatch; 

    public class MainThread extends Thread{ 
     static boolean flag=false; 
     final static CountDownLatch latch1= new CountDownLatch(1); 
     final static CountDownLatch latch2= new CountDownLatch(1); 
     final static CountDownLatch latch3= new CountDownLatch(3); 
     static MainThread t1; 
     static MainThread t2; 
     static MainThread t3; 
     static MainThread t4; 
     static MainThread t5; 

     public static void main(String args[]){ 


      t1 = new MainThread(){ 
       public void run(){ 


        System.out.println("Waiting for Thread 2"); 
        try { 
         System.out.println("THis iss before the thread 2 starts its for loop."); 
         latch2.countDown(); 
         t3.start(); 
         t4.start(); 
         t5.start(); 
         System.out.println("waiting for thread 2 to countdown"); 
         latch1.await(); 
         if(flag==true){ 
          System.out.println("successful."); 
         } 
         else{ 
          System.out.println("error."); 

         } 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        System.out.println("All the dependencies resolved."); 
        System.out.println("Waiting for the remaining threads to complete their work."); 
        try { 
         latch3.await(); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        System.out.println("All the threads have finished doing their work. Exiting now..."); 
       } 
      }; 
      Thread t2 = new MainThread(){ 
       public void run(){ 
        System.out.println("Before Starting for loop"); 
        try { 

         System.out.println("waiting for thread 1 to countdown latch2"); 
         latch2.await(); 
         System.out.println("Starting for loop"); 
         for(int i=0;i<5;i++){ 
          System.out.println("iteration: "+i); 
          try { 
           Thread.sleep(5); 
          } catch (InterruptedException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
         } 
         int x=1/0; 
         latch1.countDown(); 
         System.out.println("countdown by thread2 for latch 1 done."); 

         flag=true; 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 

        } 
        finally{ 
         latch1.countDown(); 

        } 
       } 
      }; 
      t3 = new MainThread(){ 
       public void run(){ 
        System.out.println("Running Thread 3"); 
        for(int i=0;i<10;i++){ 
         System.out.println("iteration: "+i+ " "+t3.getName()); 
         try { 
          Thread.sleep(5); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
        latch3.countDown(); 

       } 
      }; 

      t4 = new MainThread(){ 
       public void run(){ 
        System.out.println("Running Thread 4"); 
        for(int i=0;i<10;i++){ 
         System.out.println("iteration: "+i+ " "+t4.getName()); 
         try { 
          Thread.sleep(5); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 

        latch3.countDown(); 
       } 
      }; 

      t5 = new MainThread(){ 
       public void run(){ 
        System.out.println("Running Thread 5"); 
        for(int i=0;i<10;i++){ 
         System.out.println("iteration: "+i+ " "+t5.getName()); 
         try { 
          Thread.sleep(5); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 

        latch3.countDown(); 
       } 
      }; 
      t1.start(); 
      t2.start(); 


     } 





    } 

我的輸出是:

Before Starting for loop 
waiting for thread 1 to countdown latch2 
Waiting for Thread 2 
THis iss before the thread 2 starts its for loop. 
Starting for loop 
iteration: 0 
waiting for thread 2 to countdown 
Running Thread 3 
iteration: 0 Thread-2 
Running Thread 5 
iteration: 0 Thread-4 
Running Thread 4 
iteration: 0 Thread-3 
iteration: 1 
iteration: 1 Thread-3 
iteration: 1 Thread-2 
iteration: 1 Thread-4 
iteration: 2 
iteration: 2 Thread-3 
iteration: 2 Thread-2 
iteration: 2 Thread-4 
iteration: 3 
iteration: 3 Thread-3 
iteration: 3 Thread-2 
iteration: 3 Thread-4 
iteration: 4 
iteration: 4 Thread-3 
iteration: 4 Thread-4 
iteration: 4 Thread-2 
iteration: 5 Thread-3 
error. 
All the dependencies resolved. 
Waiting for the remaining threads to complete their work. 
Exception in thread "Thread-1" java.lang.ArithmeticException:/by zero 
    at ThreadProjectStructure.MainThread$2.run(MainThread.java:72) 
iteration: 5 Thread-4 
iteration: 5 Thread-2 
iteration: 6 Thread-3 
iteration: 6 Thread-4 
iteration: 6 Thread-2 
iteration: 7 Thread-3 
iteration: 7 Thread-2 
iteration: 7 Thread-4 
iteration: 8 Thread-3 
iteration: 8 Thread-2 
iteration: 8 Thread-4 
iteration: 9 Thread-3 
iteration: 9 Thread-4 
iteration: 9 Thread-2 
All the threads have finished doing their work. Exiting now... 

回答

1

你有沒有想過使用線程池?如果你可以修改你的需求來使用線程池,如果發生異常,你可以強制關閉池,因此所有的線程都會被停止。

+0

我想用鎖會比池簡單。我可以嘗試使用Pool。 有沒有使用上述代碼的解決方案? – iamP

+0

我現在沒有一個IDE可以玩,但你可以嘗試的方法之一是: 1.使用volatile布爾變量並將其設置爲false 2.如果發生異常,請將其值更改爲true 3 。在所有線程中,對於每次迭代,在進行任何處理之前(在您的情況下打印消息)檢查此變量。如果值爲真,則跳出循環。 – justcurious

0

Java沒有提供任何停止正在運行的線程的執行的直接方法,但它已經給出了中斷線程的方法。仍然不能保證停止線程的執行,只是將該中斷標誌設置爲該線程的真,並且依賴於線程來檢查標誌並拋出中斷的異常。這是停止執行線程的唯一優雅方式。 你可以通過調用handle.interrupt()來打斷另一個線程。

+0

我會試試這個。謝謝 – iamP

1

那麼,用當前的代碼,你需要讓你的視角正確。請自定義鎖定變量,否則它會讓你陷入混亂。假設以下是您的要求:只有在線程2執行成功的情況下才能繼續執行其他線程。

以下是更新後的代碼:

import java.util.concurrent.CountDownLatch; 

public class MainThread extends Thread{ 
    static boolean flag=false; 
    final static CountDownLatch waitForThread2ToFinish= new CountDownLatch(1); 
    final static CountDownLatch waitForStartSignalFromThread1= new CountDownLatch(1); 
    final static CountDownLatch latchForAllOtherThreads= new CountDownLatch(3); 
    static MainThread t1; 
    static MainThread t2; 
    static MainThread t3; 
    static MainThread t4; 
    static MainThread t5; 

    public static void main(String args[]){ 
     t1 = new MainThread(){ 
      public void run(){ 
       try { 
        System.out.println("Waiting for Thread 2 to finish"); 
        waitForStartSignalFromThread1.countDown(); 
        waitForThread2ToFinish.await(); 
        if(flag==true){ 
         System.out.println("Successful."); 
         t3.start(); 
         t4.start(); 
         t5.start(); 

         System.out.println("All the dependencies resolved."); 
         System.out.println("Waiting for the remaining threads to complete their work."); 
         try { 
          latchForAllOtherThreads.await(); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         System.out.println("All the threads have finished doing their work. Exiting now..."); 
        } 
        else{ 
         System.out.println("Error."); 

        } 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

      } 
     }; 
     Thread t2 = new MainThread(){ 
      public void run(){ 
       System.out.println("Before Starting for loop"); 
       try { 

        System.out.println("waiting for thread 1 to countdown latch2"); 
        waitForStartSignalFromThread1.await(); 
        System.out.println("Starting for loop"); 
        for(int i=0;i<5;i++){ 
         System.out.println("iteration: "+i); 
         try { 
          Thread.sleep(5); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
        int x=1/0; 

        System.out.println("countdown by thread2 for latch 1 done."); 

        flag=true; 
       } catch (Exception e) { 
        e.printStackTrace(); 

       } 
       finally{ 
        waitForThread2ToFinish.countDown(); 
       } 
      } 
     }; 
     t3 = new MainThread(){ 
      public void run(){ 
       System.out.println("Running Thread 3"); 
       for(int i=0;i<10;i++){ 
        System.out.println("iteration: "+i+ " "+t3.getName()); 
        try { 
         Thread.sleep(5); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
       latchForAllOtherThreads.countDown(); 

      } 
     }; 

     t4 = new MainThread(){ 
      public void run(){ 
       System.out.println("Running Thread 4"); 
       for(int i=0;i<10;i++){ 
        System.out.println("iteration: "+i+ " "+t4.getName()); 
        try { 
         Thread.sleep(5); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 

       latchForAllOtherThreads.countDown(); 
      } 
     }; 

     t5 = new MainThread(){ 
      public void run(){ 
       System.out.println("Running Thread 5"); 
       for(int i=0;i<10;i++){ 
        System.out.println("iteration: "+i+ " "+t5.getName()); 
        try { 
         Thread.sleep(5); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 

       latchForAllOtherThreads.countDown(); 
      } 
     }; 
     t1.start(); 
     t2.start(); 
    } 
} 
+0

線程3,4和5是獨立的線程。 我想同時運行它們。 異常處理是我在這裏面臨的唯一問題。 – iamP

+0

@iampitre然後你可以把它們從'if(flag == true)'條件中取出來。你想壓制這個異常嗎? – learningloop

+0

是的,我可以做到這一點。謝謝。 但我仍然沒有得到我想要的。 – iamP

相關問題