简体   繁体   English

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

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

I have multiple threads that are performing a search.我有多个线程正在执行搜索。 I'd like it so that when one thread completes the search and finds the solution all the other threads stop running.我希望这样当一个线程完成搜索并找到解决方案时,所有其他线程都停止运行。 This is what I have so far这是我到目前为止所拥有的

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()); 

        }
}

This compiles and prints a solution however it prints multiple solutions from each thread that is still running.这会编译并打印一个解决方案,但是它会从每个仍在运行的线程中打印多个解决方案。

My output looks something like this我的 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

Thank you for any help or suggestions.感谢您提供任何帮助或建议。

Here is a suggestion for you.这里给你一个建议。

First, you will need a method that will go through the collection of threads and signal each one to stop.首先,您将需要一个方法,该方法将 go 通过线程集合并发出信号以停止每个线程。 To do this, you'll have to make the thread collection gfg an instance variable, not a variable that is local to the main method.为此,您必须使线程集合 gfg 成为实例变量,而不是 main 方法的本地变量。

The method you write for closing the threads should iterate through each NewThread in gfg and set a boolean in it as a signal that it is time to quit.您编写的关闭线程的方法应该遍历 gfg 中的每个 NewThread 并在其中设置 boolean 作为退出的信号。

The NewThread class run() method should periodically look at this boolean. NewThread class run() 方法应定期查看此 boolean。 How often is up to you.多久取决于你。 If the run() finds the that boolean is set, then exit via a branch that goes quietly.如果 run() 发现 boolean 已设置,则通过一个安静的分支退出。

Last thing, when a NewThread completes the task, it should call your method for closing threads.最后一件事,当一个 NewThread 完成任务时,它应该调用你的方法来关闭线程。

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

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