多线程循环赛

发布于 2024-11-05 04:50:53 字数 1047 浏览 0 评论 0原文

我正在尝试测试 8 种不同的游戏算法。这些算法可以与遵循游戏界面的不同类型的游戏进行对抗。

所以,他们必须互相比赛100场比赛。我已经完成了那部分并且效果很好。现在,我正在尝试使其成为多线程,以利用朋友计算机的 8 核优势。

我对线程的使用经验很少。那么,为了使我的代码多线程化,我必须进行什么样的更改?

这是我的单线程版本的代码。

编辑:我想到的解决方案(根据我的基本知识)是创建一个 Match 类,该类需要两个玩家和他们想要玩的游戏。该类将实现 Runnable,我可以为每个游戏创建一个线程。我现在的问题是,一旦 run() 方法完成,我将如何通知结果?

谢谢

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]);
        }
    }
}

I'm trying to test 8 different game-playing algorithms. 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. 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.

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. That class would implement Runnable and I could make a thread for each of the games. My question now would be, how would I notify of the results once the run() method is finished?

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]);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情话难免假 2024-11-12 04:50:53

您不需要修改单线程实现的代码 - 您只需要创建一个实现 Runnable 接口的类并将该功能包装在 run() 方法中。然后,您可以创建一个传递该类实例的线程,并调用 Thread.start()。

请参阅以获取参考。

编辑:如何从该线程获取结果:

看起来可调用接口正是您正在寻找的。这是一个链接,其中包含有关如何使用它的基本说明。它需要了解如何使用 Runnable 接口创建没有结果的多线程应用程序的基本知识,因此我建议阅读 这个

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. Then you can make a Thread passing in an instance of that class, and call 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. 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.

芯好空 2024-11-12 04:50:53
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

当前设计的问题是游戏对象看起来不是线程安全的。您可能需要为每个 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. You'll probably want a new game object for each Runnable.

大姐,你呐 2024-11-12 04:50:53

没有灵丹妙药。在问这个问题之前,您需要了解基础知识。首先阅读 MSDN 上的这篇文章: http://msdn.microsoft.com/en -us/magazine/cc163744.aspx。对于 Java 来说,这几乎是一样的。

完成后,回来询问有关您自己尝试时遇到的问题的另一个问题。

当然,您可能会在这里得到另一个答案,告诉您该怎么做。但是不要阅读该答案。在进一步了解之前了解基础知识至关重要。否则,当您的应用程序停止工作时,您将完全迷失方向。

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. It's pretty much the same for 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文