gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:36:13 25 4
gpt4 key购买 nike

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

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

}
}

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

我的输出看起来像这样

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

感谢您的帮助或建议。

最佳答案

这是给您的建议。

首先,您需要一个方法来遍历线程集合并向每个线程发出停止信号。为此,您必须将线程集合 gfg 设置为实例变量,而不是 main 方法的本地变量。

您编写的用于关闭线程的方法应该迭代 gfg 中的每个 NewThread 并在其中设置一个 boolean 值作为退出时间的信号。

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

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

关于java - java中如何在一个线程完成时结束其他线程的处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61034618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com