2013-05-21 151 views
-4

我是新的線程,我需要修復這個錯誤,你能幫我嗎?線程問題

對不起是我的錯,這是我必須做的:

此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); 
    } 
} 
+3

這是很多代碼。你能提供一個最小的工作例子嗎?請查看http://sscce.org – cyroxx

+1

請修剪/縮小代碼的大小。方法setError,getName,getWin,setWin與你所遇到的問題有什麼關係?如果代碼太多,人們會忽略這個問題。 –

+0

例如。你的代碼中的記錄器對你所問的問題沒有影響。去掉它。 –

回答

1

該代碼在main創建10個線程。每個線程在run中播放1000場比賽。總共有10 * 1000 = 10000個遊戲。

要更改遊戲數量,只需更改這些數字即可。

1

如果您需要執行run 1000次使用10個線程,請考慮以下選項。

  1. 使每個線程執行100次。
  2. 使用線程間共享的靜態計數器,在計數器達到1000時停止執行。請確保您使用計數器的AtomicInteger以確保它是線程安全的。
0

在代碼中,您正在創建10個主線程。 只需更改共享字段和方法的線程數並使用同步。