用于命名线程

时间:2016-03-30 21:26:02

标签: java multithreading

因为我有一个帖子而且在我的主程序中我想创建同一个东西的多个线程而我不想要同一个名字我试图为每个线程创建唯一的名称,例如Player_1Player_2等。

问题是它抛出了重复的局部变量,我知道这是因为我使用相同的变量,但我不知道如何在没有编写它的情况下为线程创建多个名称。这是代码。

原始问题

for (int l=0; l < noPlayers; l++){
    String name = P + "" + (l + 1);
    System.out.println(name);
    Player name = new Player(TURN);
}

* Player是扩展的主题,而TURN只是一个将被处理的变量。

更新了问题

   import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

class Referee extends Thread{


}

class Player extends Thread{
    private int TURN;
    public  Player(int TURN) {
        this.TURN = TURN;
       }

    public void run(){

        int win = 0;
        int lose = 0;
        int draw = 0;
        boolean k = true;
        int j = 0; 
    for (j=0;j<=TURN;j++){
             String [] arr = {"ROCK", "PAPER", "SCISSORS"};
             Random Pick = new Random();

             // randomly selects an index from the arr
             int select = Pick.nextInt(arr.length); 

             // prints out the value at the randomly selected index
             System.out.println("Random String selected: " + arr[select]); 
        }
    }   
}
public class MainApp {

    public static void main(String[] args) {
        int TURN = 0;
        int No_Players = 0;
        int i=0;
        boolean h= true;
        int j=0;
        boolean k = true;


        System.out.println("Welcome to Rock-paper-scissors, this is a tutorial of threads");  
        Scanner number = new Scanner( System.in );
        System.out.println("Insert the number of players that will play");

        while (h){
            while (!number.hasNextInt()){
            System.out.println("You didnt insert a number please try again");
            number.next();
            }
            if(number.hasNextInt()){
             j = number.nextInt();
                if(j>1){
                    No_Players = j;
                    h=false;    
                }
                else {  
                    System.out.println("You need a value bigger than 1");
                }
            }
        }   
        System.out.println("Please enter how many turns you want each player to play");

        while (k){
            while (!number.hasNextInt()){
            System.out.println("You didnt insert a number please try again");
            number.next();
            }
            if(number.hasNextInt()){
             i = number.nextInt();
                if(i>0){
                    TURN = i;
                    k=false;    
                }
                else {  
                    System.out.println("You need a value bigger than 0");
                }
            }
        }   
        System.out.println("This game will have " + No_Players +" players and each one will have " + TURN + " turns");

        Map<String,Thread> map = new HashMap<>();
        for (int l = 0; i < No_Players; ++i) {
           String name = "Player_" + i;
           Player player = new Player(TURN);

           player.setName(name);
           map.put(name, player);
           player.start();
        }

    }   
}

**因为一些误解而发布了整个代码。

1 个答案:

答案 0 :(得分:0)

假设Player实现了Runnable。

Map<String,Thread> map = new HashMap<>();
for (int i = 0; i < numPlayers; ++i) {
   String name = "Player_" + i;
   Player player = new Player(TURN);
   Thread t = new Thread(player):
   t.setName(name);
   map.put(name, t);
   t.start();
}

然后您可以按播放器名称检索线程。如果需要,您还可以添加一个线程组。

根据播放器扩展线程的注释(编辑:修复了Map定义以使用Player而不是Thread)。

Map<String,Player> map = new HashMap<>();
for (int i = 0; i < numPlayers; ++i) {
   String name = "Player_" + i;
   Player player = new Player(TURN);

   player.setName(name);
   map.put(name, player);
   // thread may need to be started elsewhere depending upon the requirement
   player.start();
}

在其他地方获得特定玩家,只需:

Player p = map.get(playerName);

基于评论,这里是一个工作示例,显示播放器,使用上面建议的Map扩展Thread,并在Player对象上使用.getName()方法显示播放器。

public class TestPlayer
{

public TestPlayer()
{
}


public static void main(String[] args)
{
    int numPlayers = 5;

    Map<String,Player> map = new HashMap<>();
    for (int i = 0; i < numPlayers; ++i) {
       String name = "Player_" + i;
       Player player = new Player(0);

       player.setName(name);
       map.put(name, player);
       // thread may need to be started elsewhere depending upon the requirement
       player.start();
    }

    for (Player p : map.values()) {
        System.out.println("Found player : " + p.getName());
    }


}


static class Player extends Thread
{
    private int turn;

    public Player(int turn)
    {
        this.turn = turn;
    }

    @Override
    public void run()
    {
        System.out.println("Running player " + 
                Thread.currentThread().getName() +
                " turn " + this.turn);
    }
}

}

输出: 运行播放器Player_1变为0 运行播放器Player_3变为0 运行播放器Player_0变为0 运行播放器Player_2变为0 运行播放器Player_4变为0 找到了玩家:Player_3 找到了玩家:Player_2 找到了玩家:Player_4 找到了玩家:Player_1 找到了玩家:Player_0