5 Replies Latest reply: Dec 27, 2007 8:10 AM by 807603 RSS

    How to stop the 2nd thread when 1st thread caught exception.

    807603
      Hello Friends,

      I have written a java program using Thread.
      In this program i have created two separate threads.
      For example Thread1 and Thread2.

      During execution of both threads,

      If any exception comes on Thread1 or Thread2, the other thread should not be continued its execution. Suddenly the other thread has to interrupted its execution.

      How to do this. If any body know the way to do this, please help me.

      Rgds
      tskarthikeyan
        • 1. Re: How to stop the 2nd thread when 1st thread caught exception.
          Jörg
          boolean exceptionThrown= false;
          Thread t1 = new Thread(new Runnable() {
            public void run () {
          .
          .
          .
              if (exceptionThrown) return; // Check regularly
          .
          .
          .
              catch (Exception e) {
                exceptionThrown= true;
              }
            }
          });
          t1.start();
          Thread t2 = new Thread(new Runnable() {
            public void run () {
          .
          .
          .
          // Same with thread 2.
          • 2. Re: How to stop the 2nd thread when 1st thread caught exception.
            807603
            Hello Friend,

            Thanks for yr reply. I have tried with your example. But i dont get the result which i exactly expect.
            when the exceptionThrown boolean variable becomes "true", then the first thread execution interrupted. But the second thread continues.


            I have added my code snippet for this, could you check it out.

            Rgds
            tskarthikeyan

            public class ThreadComm {
                 public static boolean exceptionThrown= false;
            
                 public static void main(String[] args) {
                      
                      Thread t1 = new Thread(new Runnable() {
                           public void run () {
                                try {
                                     if (exceptionThrown) {
                                          System.out.println("First Thread Interrupted");
                                          return;
                                     }
                                     for(int i=1;i<=500;i++){
                                          System.out.println("FirstThread>>"+i);
                                          if(i==250) {
                                               throw new Exception("First Thread Interrupted");
                                          }
                                     }
                                }catch(Exception e) {
                                     exceptionThrown = true;
                                }
                           }
                      });
                      t1.start();
                      
                      Thread t2 = new Thread(new Runnable() {
            
                           public void run() {
                                try {
                                     if (exceptionThrown) {
                                          System.out.println("Second Thread Interrupted");
                                          return;
                                     }
                                     for(int i=1;i<=1000;i++){
                                          System.out.println("SecondThread>>"+i);
                                          if(i==777) {
                                               throw new Exception("Second Thread Interrupted");
                                          }
                                     }
                                }catch(Exception e) {
                                     exceptionThrown = true;
                                }
                                
                           }
                           
                      });
                      t2.start();
                 }
                 
            }
            • 3. Re: How to stop the 2nd thread when 1st thread caught exception.
              807603
              Try this. It was working Fine.


              public class ThreadComm {
                   public static volatile boolean exceptionThrown= true;

                   public static void main(String[] args) {
                        
                        Thread t1 = new Thread(new Runnable() {
                             public void run () {
                                  try {
                                       
                                       /*if (exceptionThrown) {
                                            System.out.println("First Thread Interrupted");
                                            return;
                                       }*/
                                       for(int i=1;i<=5000;i++){
                                            System.out.println("FirstThread>>"+i);
                                            if(i==2500) {
                                                 throw new Exception("First Thread Interrupted");
                                            }
                                       }
                                       
                                  }catch(Exception e) {
                                       exceptionThrown = false;
                                  }
                             }
                        });
                                  
                        Thread t2 = new Thread(new Runnable() {
                             public void run() {
                                  try {
                                       /*if (exceptionThrown) {
                                            System.out.println("Second Thread Interrupted");
                                            return;
                                       }
                                       for(int i=1;i<=1000;i++){
                                            System.out.println("SecondThread>>"+i);
                                            if(i==777) {
                                                 throw new Exception("Second Thread Interrupted");
                                            }
                                       }*/
                                       int i=0;
                                       while(exceptionThrown && i<=5000) {
                                            System.out.println("SecondThread>>"+i);
                                            if(i==4000) {
                                                 throw new Exception("Second Thread Interrupted");
                                            }
                                            i++;
                                       }
                                       
                                  }catch(Exception e) {
                                       exceptionThrown = true;
                                  }
                                  
                             }
                             
                        });
                        t1.start();
                        t2.start();
                        
                   }
                   
              }
              • 4. Re: How to stop the 2nd thread when 1st thread caught exception.
                807603
                Hello Friends,

                Its working. moreover i have to be done some changes in this code.
                Thanks a lot for your kind help.


                Rgds
                tskarthikeyan
                • 5. Re: How to stop the 2nd thread when 1st thread caught exception.
                  807603
                  Hello Friends,

                  I have a doubt. we are using while check for throwing exception. But if i call a function instead printing numbers, how the exceptionThrown boolean variable will be checked?.

                  I think, i call a function, its running separately. so we could not check the exceptionThrown boolean variable every time. Could you clarify my doubt.

                  Rgds
                  tskarthikeyan