2014-02-17 86 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块 - 也许这就是你应该开始。

相关问题