英文:
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());
}
}
请注意,这是您提供的代码的翻译版本,与原始代码一样,只是将注释中的特殊字符进行了修正,以便在中文环境中能够正确显示。如果您有进一步的问题或需要帮助,请随时提问。
英文:
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
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.
答案1
得分: 0
这里有一个建议供您参考。
首先,您需要一个方法,该方法将遍历线程集合并发出停止信号。为了做到这一点,您需要将线程集合 gfg 设为实例变量,而不是局部于主方法的变量。
您为关闭线程编写的方法应该遍历 gfg 中的每个 NewThread,并在其中设置一个布尔值作为信号,表示是时候退出了。
NewThread 类的 run() 方法应该定期检查这个布尔值。检查频率由您决定。如果 run() 发现布尔值已设置,那么通过一个静默退出的分支进行退出。
最后一件事,在 NewThread 完成任务时,应调用您的关闭线程方法。
英文:
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. 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.
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.
The NewThread class run() method should periodically look at this boolean. How often is up to you. If the run() finds the that boolean is set, then exit via a branch that goes quietly.
Last thing, when a NewThread completes the task, it should call your method for closing threads.
专注分享java语言的经验与见解,让所有开发者获益!
评论