Multithreaded Round Robin Tournament

I am trying to test 8 different game algorithms. These algorithms can play against each other in different games that follow the game interface.

Thus, they have to play against each other in 100 games. I have done this part and everything works fine. Now I'm trying to do this multi-threaded in order to use a friend's 8-core computer.

I have very little experience with threads. So, what changes should I make to make my code multithreaded?

Here is the code for my single threaded version.

EDIT: The solution I was thinking about (with my basic knowledge) is to create a Match class that accepts two players and the games they want to play. This class will implement Runnable, and I could create a thread for each of the games. My question now would be, how would I notify the results after the run () method completes?

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]); } } } 
+7
source share
3 answers
 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 will probably need a new game object for each Runnable.

+2
source

You do not need to change the code for a single-threaded implementation - you just need to create a class that implements the Runnable interface and transfers this functionality to the run () method. Then you can make a thread passing in an instance of this class and call Thread.start() .

See this for reference.

Edit: How to get results from this thread:

Callable Interface seems to be what you are looking for. Here's a link with a basic explanation of how to use it. This requires basic knowledge of how the Runnable interface is used to create a multi-threaded application without results, so I would recommend reading this this first .

+3
source

There is no magic bullet. Before asking this question, you need to know the basics. Start by reading this article on MSDN: http://msdn.microsoft.com/en-us/magazine/cc163744.aspx . This is almost the same for Java.

When done, go back and ask another question regarding the problem you received with your own attempt.

Of course, you can get another answer that shows what to do. But please do not read this answer. It is very important that you understand the basics before moving on. Otherwise, you will be completely lost when your application stops working.

+1
source

All Articles