multithreading - Counting the number of games played in 1 second using Java Threads -


I want to know how many games my computer can play in 1000 ms. I tested without using threads (this 13k plays) Now I think I am using threads, I still get this. Since I do not have much experience with Java sources, I think I am doing something wrong, but I can not get it.

Thanks in advance

  Public Class SpeedTest & lt; T board game extension & gt; Runable {public fixed int game played = 0; Private ElapsedTimer Timer; Private double maximum time; Private BoardAgent & lt; T & gt; Agent; Private board game & lt; T & gt; sport; Public SpeedTest (board game & lt; T & gt; Games, Elapsed Timer Timer, Double Maxtime, Random RNG) {this.game = game; This.timer = Timer; This.maxTime = maxime; This.agent = New RandomAgent & lt; T & gt; (RNG); } @ Override Public Wide Run (While {true} {BoardGame & lt; T & gt; NewBoard = game.copy (); While (NewBoard.isGameOver ()) newBoard.makeMove (agent.move (newBoard)); GamesPlayed ++; If (timer.packaged milliseconds ()> maxime) {break; }}} Public static zero main (string [] args) {random RNG = new random (); Boardgame & LT; Connect4 & gt; Game = New Connect 4 (6, 7); Double Maximum Time = 1000; ElapsedTimer timer = new ElapsedTimer (); SpeedTest & LT; Connect4 & gt; SpeedTest1 = new SpeedTest & lt; Connect 4 & gt; (Game, Timer, Maxime, RNG); SpeedTest & LT; Connect4 & gt; SpeedTest2 = new SpeedTest & lt; Connect 4 & gt; (Game, Timer, Maxime, RNG); Thread t1 = new thread (speedTest1); Thread T2 = New thread (speedTest2); T1.start (); T2.start (); Try {Thread.sleep (maximum) maximum time); } Grip (Interrupted e) e.printStackTrace (); } System.out.println ("Games:" + SpeedTest.gamesPlayed); }}    

I suspect that due to which you are not seeing any speed It is that your application is using only 1 physical processor, if it is using only one processor, then two threads will not run in parallel. Instead, the processor will be "time-skiing" between two threads.

What can you do about this?

  • Processor to run on dual core etc. Or if you have a processor machine with HT support, enable HT.

  • Run the test for longer time; Like many minutes.

    For this reason I recommend that it be a JVM warm-up effect. When a JVM starts a new app, it has to load many classes and JIT has to be compiled behind the scenes. These tasks will be largely (if not completely) single-threaded. Running tests in the long run reduces the contribution of "hot" overheads in the average time of "sports".


    There is a fix you should make to make program thread-safe change

      public fixed int game = 0;   

    to

      Private Static Last Atomicator Game Download = New AtomicInteger ();   

    And then to increase the counter, use getAndIncrement () and use intValue () to fetch its value . (It is easy to keep each thread in its counter and summarize it at the end).


    However, I strongly suspect that this change (or alternatives to Eric) The difference will be a little bit of the results you are seeing now that I am sure this is either:

    • The above JVM warmup issue,
    • High object creation rates and / or piles of hunger Results of, or
    • between your game instances some hidden Singh Transition problem.

Comments