-4

我是 Threads 的新手,我需要修复这个错误,你能帮我吗?

对不起,这是我的错,这是我必须做的:

此 Web 应用程序必须执行 1000 个游戏并显示输出结果。

我再次道歉。

谢谢

/**
 * 
 * Automatic agent to play 1000 games
 * 
 */
public class AutoPlayer implements Runnable {

    private RequestDispatcher requestDispatcher;

    public AutoPlayer(RequestDispatcher requestDispatcher) {
    this.requestDispatcher = requestDispatcher;
    }


    public static void main(String[] args) {

    HashMap<String, Game> games = new HashMap<String, Game>();

    RequestDispatcher rd = new RequestDispatcher(games);

    Vector<Thread> threads = new Vector<Thread>();

    for (int i = 0; i < 10; i++) {
        AutoPlayer autoPlayer = new AutoPlayer(rd);
        Thread thread = new Thread(autoPlayer);
        threads.add(thread);
        thread.start();
    }

    for (int i = 0; i < threads.size(); i++) {
        try {
        threads.get(i).join();
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
    }

    }



    @Override
    public void run() {

    PlayResponse response = null;

    for (int i = 0; i < 1000; i++) {
        byte[] numbers = requestDispatcher.cardRequest();

        try {
        response = new PlayResponse();
        requestDispatcher.process("Lucky", 10, numbers, response);
        } catch (UnknownGameException e) {
        e.printStackTrace();
        }

        if (response != null) {
        System.out.println("[" + Thread.currentThread() + "] total requests:" + requestDispatcher.generatedCards);
        }
    }
    }

}



public class Game {

    private String name;
    private int gamesPlayed;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public int getGamesPlayed() {
    return gamesPlayed;
    }

    public void setGamesPlayed(int gamesPlayed) {
    this.gamesPlayed = gamesPlayed;
    }

}



/**
 * 
 * Response to a Play request
 * 
 */
public class PlayResponse {

    private boolean error;
    private long win;

    public boolean isError() {
        return error;
    }
    public void setError(boolean error) {
        this.error = error;
    }
    public long getWin() {
        return win;
    }
    public void setWin(long win) {
        this.win = win;
    }

}




/**
 * 
 * Object that processes play requests, calculates outcomes and returns results.
 * 
 */
public class RequestDispatcher {

    List<String> list = Arrays.asList("Lucky", "Happy", "Extra");

    final int CARD_SIZE = 15;

    public String GAME_UNAVAILABLE = "Error: Game not available";

    Map<String, Game> games;

    long generatedCards;

    Logger logger = Logger.getLogger(getClass().getName());

    Random r = new Random();

    public RequestDispatcher(HashMap<String, Game> games) {
    this.games = games;

    }

    public byte[] cardRequest() {
    byte[] result = createCard();
    generatedCards++;

    return result;
    }

    private byte[] createCard() {

    byte[] result = new byte[CARD_SIZE];

    r.nextBytes(result);

    return result;
    }

    public void process(String s, int i, byte[] bb, PlayResponse pr0) throws UnknownGameException {

    if (!list.contains(s)) {
        logger.log(Level.SEVERE, GAME_UNAVAILABLE);
        throw new UnknownGameException(GAME_UNAVAILABLE);
    }

    Game game = games.get(s);

    if (game != null) {
        game.setGamesPlayed(game.getGamesPlayed() + 1);
    } else {
        Game g = new Game();
        g.setName(s);
        games.put(s, g);
        g.setGamesPlayed(0);
    }

    pr0.setWin(r.nextInt(3) * i);
    pr0.setError(false);

    }
}


public class UnknownGameException extends Exception {

    private static final long serialVersionUID = 2380720995275983122L;

    public UnknownGameException(String s) {
    super(s);
    }
}
4

3 回答 3

1

如果您需要run使用 10 个线程执行 1000 次,请考虑以下选项。

  1. 使每个线程执行 100 次。
  2. 使用在线程之间共享的静态计数器,当计数器达到 1000 时停止执行。确保使用AtomicInteger计数器以确保它是线程安全的。
于 2013-05-21T12:45:02.043 回答
1

该代码在main. 每个线程在run. 总共有 10 * 1000 = 10000 场比赛。

要更改游戏数量,只需更改这些数字即可。

于 2013-05-21T12:43:47.673 回答
0

在代码中,您将在 main 中创建 10 个线程。只需更改线程数并为共享字段和方法使用同步。

于 2013-05-21T13:00:17.027 回答