简体   繁体   English

多线程循环锦标赛

[英]Multithreaded Round Robin Tournament

I'm trying to test 8 different game-playing algorithms.我正在尝试测试 8 种不同的游戏算法。 These algorithms can play against each other different type of games that follow an Game interface.这些算法可以与遵循游戏界面的不同类型的游戏相互对抗。

So, they have to play against each other 100 games.所以,他们必须互相对抗100场比赛。 I've done that part and it works fine.我已经完成了那部分,它工作正常。 Now, I'm trying to make it multithreaded, to take advantage of the 8-cores of a computer of a friend.现在,我正在尝试使其成为多线程,以利用朋友计算机的 8 核。

I have very little experience working with threads.我很少有使用线程的经验。 So, what kind of changes would I have to make in order to make my code multithreaded?那么,为了使我的代码成为多线程,我必须进行哪些更改?

Here is the code for my single threaded version.这是我的单线程版本的代码。

EDIT: The solution I thought of (with my basic knowledge) is about making a Match class, which takes two players and the game they want to play.编辑:我想到的解决方案(以我的基本知识)是关于制作比赛 class,这需要两名玩家和他们想要玩的游戏。 That class would implement Runnable and I could make a thread for each of the games. class 将实现 Runnable,我可以为每个游戏创建一个线程。 My question now would be, how would I notify of the results once the run() method is finished?我现在的问题是,一旦 run() 方法完成,我将如何通知结果?

Thanks谢谢

for (int p1 = 0; p1 < allPlayers.length; p1++)
{
    for (int p2 = p1 + 1; p2 < allPlayers.length; p2++)
    {    
        for (int t = 0; t < trials; t++)
        {
            int player1 = t % 2 == 0 ? p1 : p2;
            int player2 = t % 2 == 0 ? p2 : p1;
            Player[] players = new Player[] { allPlayers[player1], allPlayers[player2] };
            game.newGame();

            while (!game.isFinished())
                game.playNthMove(players[game.currentPlayer()].move(game));

            data[p1][p2][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 0 : 1]);
            data[p2][p1][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 1 : 0]);
        }
    }
}

You shouldn't need to modify the code for the single-threaded implementation - you should just need to make a class that implements the Runnable interface and wrap that functionality in a run() method.您不需要修改单线程实现的代码 - 您只需要创建一个实现 Runnable 接口的 class 并将该功能包装在 run() 方法中。 Then you can make a Thread passing in an instance of that class, and call Thread.start() .然后,您可以创建一个 Thread 传递该 class 的实例,并调用Thread.start()

See this for a reference.请参阅内容以供参考。

Edit: How to get results from that thread:编辑:如何从该线程获取结果:

It looks like the Callable Interface is what you're looking for.看起来 Callable Interface 是您正在寻找的。 Here's a link with a basic explanation on how to use it.这是一个 链接,其中包含有关如何使用它的基本说明。 It requires a basic knowledge of how the Runnable interface is used for creating a multi-threaded application without results, so I'd recommend reading this first.它需要有关如何使用 Runnable 接口来创建没有结果的多线程应用程序的基本知识,因此我建议您先阅读本文

Collection<Callable<Void>> tasks = new ArrayList<Callable<Void>();
for (int p1 = 0; p1 < allPlayers.length; p1++)
{
    for (int p2 = p1 + 1; p2 < allPlayers.length; p2++)
    {    
        for (int t = 0; t < trials; t++)
        {
            final int player1 = t % 2 == 0 ? p1 : p2;
            final int player2 = t % 2 == 0 ? p2 : p1;
            final Player[] players = new Player[] { allPlayers[player1], allPlayers[player2] };
            final int trial = t;
            tasks.add(new Callable<Void>() {
               public Void call() {
                 game.newGame();

                 while (!game.isFinished())
                    game.playNthMove(players[game.currentPlayer()].move(game));

                 data[p1][p2][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 0 : 1]);
                 data[p2][p1][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 1 : 0]);
                 return null;
              }
           });
        }
    }
}
executor.invokeAll(tasks); // called on an exector, will wait for all tasks to complete

The problem with the current design is that the game object does not look thread safe.当前设计的问题是游戏 object 看起来不是线程安全的。 You'll probably want a new game object for each Runnable.您可能需要为每个 Runnable 提供一个新游戏 object。

There is not magic bullet.没有灵丹妙药。 You need to know the basics before asking that question.在问这个问题之前,您需要了解基础知识。 Start by reading this article at MSDN: http://msdn.microsoft.com/en-us/magazine/cc163744.aspx .首先阅读 MSDN 上的这篇文章: http://msdn.microsoft.com/en-us/magazine/cc163744.aspx It's pretty much the same for Java. Java 几乎相同。

When done, come back and ask another question regarding a problem you got with your own attempt.完成后,回来就您自己尝试遇到的问题提出另一个问题。

Sure, you might get another answer here showing you what to do.当然,您可能会在这里得到另一个答案,告诉您该怎么做。 But please , do not read that answer.但是,不要阅读该答案。 It's vital that you understand the basics before going further.在继续之前了解基础知识至关重要。 Otherwise you'll be completely lost when your app stops working.否则,当您的应用程序停止工作时,您将完全迷失方向。

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

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