繁体   English   中英

java中一个线程完成时如何结束其他线程的处理

[英]How to end the processing of other threads when one thread completes in java

我有多个线程正在执行搜索。 我希望这样当一个线程完成搜索并找到解决方案时,所有其他线程都停止运行。 这是我到目前为止所拥有的

import java.util.Scanner;


class NewThread extends Thread  
{ 

    int n = 4;

    NewThread(String threadname, ThreadGroup tgob, int n) 
    { 
        super(tgob, threadname);
        this.n = n;
        start(); 
    } 
public void run() 
    { 

        System.out.println("Thread running");

                   long timestamp1 = System.currentTimeMillis();

                   System.out.println("Solution to "+ n +" queens using hill climbing search:");

                   HillClimbingSearch hcs = new HillClimbingSearch(n);

                   hcs.runSearch();

                   if (hcs.getFinalSolution() != null)
                   hcs.printState(hcs.getFinalSolution());

                   //Printing the solution          
                   long timestamp2 = System.currentTimeMillis();
                   long timeDiff = timestamp2 - timestamp1;
                   System.out.println("Execution Time: "+timeDiff+" ms");

                   System.out.println(Thread.currentThread().getName() + 
                   " finished executing"); 
    } 
}  

public class Main extends Thread{

    public static void main(String[] args) {


            int n = 0; 
            try (Scanner s=new Scanner(System.in)) {
                while (true){
                    System.out.println("Enter the number of Queens :");
                    n = s.nextInt();
                    if ( n == 2 || n ==3) {
                        System.out.println("No Solution possible for "+ n +" Queens. Please enter another number");
                    }
                    else
                        break;
                }
            }

            // creating the thread group 
            ThreadGroup gfg = new ThreadGroup("parent thread group"); 

            NewThread t1 = new NewThread("one", gfg, n); 
            System.out.println("Starting one"); 
            NewThread t2 = new NewThread("two", gfg, n); 
            System.out.println("Starting two"); 
            NewThread t3 = new NewThread("three", gfg, n); 
            System.out.println("Starting three"); 

            boolean keepRunning = true;

            while(keepRunning){
                if (t1.isAlive() && t2.isAlive() && t3.isAlive()){
                    continue;
                } else {
                    t1.interrupt();
                    t2.interrupt();
                    t3.interrupt();
                    keepRunning = false;
                }
            }

            // checking the number of active thread 
            System.out.println("number of active thread: "
                               + gfg.activeCount()); 

        }
}

这会编译并打印一个解决方案,但是它会从每个仍在运行的线程中打印多个解决方案。

我的 output 看起来像这样

Enter the number of Queens :
5
Starting one
Thread running
Starting two
Starting three
Thread running
Thread running
Solution to 5 queens using hill climbing search:
Solution to 5 queens using hill climbing search:
Solution to 5 queens using hill climbing search:


0 1 0 0 0 
0 0 0 1 0 
1 0 0 0 0 
0 0 1 0 0 
0 0 0 0 1 
0 1 0 0 0 
0 0 0 1 0 
1 0 0 0 0 
0 0 1 0 0 
0 0 0 0 1 
0 1 0 0 0 
0 0 0 1 0 
1 0 0 0 0 
0 0 1 0 0 
0 0 0 0 1 
Execution Time: 81 ms
Execution Time: 72 ms
Execution Time: 98 ms
one finished executing
two finished executing
three finished executing
number of active thread: 0

感谢您提供任何帮助或建议。

这里给你一个建议。

首先,您将需要一个方法,该方法将 go 通过线程集合并发出信号以停止每个线程。 为此,您必须使线程集合 gfg 成为实例变量,而不是 main 方法的本地变量。

您编写的关闭线程的方法应该遍历 gfg 中的每个 NewThread 并在其中设置 boolean 作为退出的信号。

NewThread class run() 方法应定期查看此 boolean。 多久取决于你。 如果 run() 发现 boolean 已设置,则通过一个安静的分支退出。

最后一件事,当一个 NewThread 完成任务时,它应该调用你的方法来关闭线程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM