我目前正在开发一个简单的国际象棋人工智能。 (计算 future 可能的回合,对它们进行评分,选择最好的一个,+一些技巧,这样你就不必计算每个回合)。该代码是用 Java 编写的,我使用的是 Netbeans。为了使计算更快,我使用多线程。代码的工作原理大致如下:
- Main 函数首先进行一些计算,然后启动 8 个线程。
- 线程执行主计算
- 完成后,他们在 boolean 数组 (finished[]) 中设置一个 boolean 值 true。该数组位于“主类”中(如果您这样调用它),主函数也位于其中。
- 在这段时间里,主函数一直在等待并不断检查 finish[] 数组的每个值是否为 true。如果是这种情况,它将继续工作。
现在我遇到了一个奇怪的问题。该代码在我的 PC 上完美运行,但是当我在笔记本电脑上运行完全相同的代码时,在完成的 [] 数组的所有值都为 true 后, main 函数将不会继续其工作。我已经对代码进行了一些更改,因此我可以使用不同数量的线程进行尝试,但结果始终相同。
我完全不知道这里发生了什么,如果你们有人有任何答案和/或建议,我将非常感激!
如果您需要更多信息,请询问,我会尽力而为。 :)
(很抱歉可能出现语法错误,英语不是我的母语,但我正在尽力。;))
所以我被要求展示一些我在程序中使用的代码:
(也许首先是一个警告,是的,我仍然是 Java 中的大菜鸟,这是我第一次使用线程,所以如果您看到我可能犯的可怕错误,请不要感到震惊。xD)
主类看起来像这样:
public class Chess_ai_20 {
static boolean finished[] = new boolean[8];
Distributor[] Distributors = new Distributor[8];
...
public static void main(String[] args) {
boolean testing=false;
...
//some calculations and other stuff
...
Distributors[0] = new Distributor(...., "0"); //the String "0" will be the thread name.
Distributors[1] = new ...
...
Distributors[7] = new Distributor(...., "7");
for (int i = 0; i < 8; i++) {
Distributoren[i].start();
}
testing=false;
while(testing==false){
if(finished[0]==true && finished[1]==true && ... && finished[7]==true){
testing=true; //That's the point where I get stuck I suppose
}
}
System.out.println("I made it!");
}
public static void setFinished(int i) {
finished[i] = true;
System.out.println("finished [" + i + "] = " + finished[i]);
System.out.println(Arrays.toString(finished)); //To check how many values already are true
}
}
然后我们当然得到了“Distributor”类
public class Distributor extends Thread {
Thread t;
String threadname;
boolean running=false;
...
Distributor(......, String s) {
threadname=s;
...
...
}
@Override
public void start() {
running=true;
if (t == null) {
t = new Thread(this,threadname);
t.start();
}
}
@Override
public void run() {
if(running){
...
//Do the main calculations etc.
...
//All the Calculations habe been done at this point
Chess_ai_20.setFinished(Character.getNumericValue(threadname.charAt(0))); //Set the value of finished[] true in the main class
running=false;
}
}
}
最佳答案
正如其他人提到的,使用 Future
会更加简单和容易理解。以下是演示如何重写代码的片段。查看code in action .
首先,编写一个Callable
来定义您想要执行的任务。
public class MyCallable implements Callable<Boolean> {
@Override
public Boolean call() {
// Do some job and return the result.
return Boolean.TRUE;
}
}
然后,您将此任务提交给 Executor
。 JDK中有很多Executor。您想通过Concurrency Tutorial首先。
ExecutorService executor = Executors.newFixedThreadPool(Runtime
.getRuntime().availableProcessors());
List<Callable<Boolean>> callables = new ArrayList<>();
for (int counter = 0; counter < 8; counter++) {
callables.add(new MyCallable());
}
List<Future<Boolean>> futures = executor.invokeAll(callables);
for (Future<Boolean> future : futures) {
System.out.println(future.get()); // You'd want to store this into an array or wherever you see fit.
}
executor.shutdown();
请记住 futures returned by the executor are in the same order as the Callable
s you submitted (or added)到Collection
(在本例中为ArrayList
)。因此,您无需担心返回索引、ID 甚至线程名称(如果您已分配)来映射相应的结果。
关于java - 完全相同的代码(使用多线程)无法在两台不同的计算机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35803771/