2015-09-02 66 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

相关问题