java - Problems with threads -
i'm newer threads , need fix error me?
sorry fault have do:
this web application must execute 1000 games , show output results.
my apologize again.
thanks
/** * * automatic agent 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>(); (int = 0; < 10; i++) { autoplayer autoplayer = new autoplayer(rd); thread thread = new thread(autoplayer); threads.add(thread); thread.start(); } (int = 0; < threads.size(); i++) { try { threads.get(i).join(); } catch (interruptedexception e) { e.printstacktrace(); } } } @override public void run() { playresponse response = null; (int = 0; < 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 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 processes play requests, calculates outcomes , 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); } }
the code creates 10 threads in main
. each thread plays 1000 games in run
. gives total of 10 * 1000 = 10000 games.
to change number of games change numbers.
Comments
Post a Comment