如何在完成其他线程后停止一个线程?

时间:2014-02-17 12:25:10

标签: java multithreading

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完成执行后立即停止线程,我该怎么做?

7 个答案:

答案 0 :(得分:1)

您可以使用 volatile布尔变量 并将其设置为 false 。完成线程y后,将其设为 true 。在条件检查停止所需的线程。

答案 1 :(得分:0)

您可以停止主线程的主处理,直到线程y完成,然后停止第n个线程。 使用以下内容:

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");
    }   
}

答案 2 :(得分:0)

在原则级别,使用线程Y在完成之前设置的标志,并且线程TH每次都检查它。当线程Y将其设置为true或false并且线程TH读取该更改时,则可以将其停止。

答案 3 :(得分: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");
    }   

}

答案 4 :(得分: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();
}
    }


                    }};

答案 5 :(得分: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");
    }
}

答案 6 :(得分:0)

  

这里我想在线程y完成后立即停止Thread   执行,我该怎么做?

Java为您提供了所需的方法。您需要做的就是填写“TODO”块。

从JDK文档中引用interrupt方法:

  

中断此主题。

     

... [snip]

     

如果在调用wait()时阻塞了这个线程,请等待(long),   或者等待(long,int)Object类或join()的方法,   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");
    }   
}

故事的寓意是:看看那些自动生成的捕获区块 - 也许这就是你应该开始的地方。