Try to make just 2 threads - should remove the scheduler overhead.

On Nov 15, 1:09 am, Alexey Zinger <[EMAIL PROTECTED]> wrote:
> Yeah, that makes sense.
>
> Alexey
> 2001 Honda CBR600F4i (CCS)
> 1992 Kawasaki 
> EX500http://azinger.blogspot.comhttp://bsheet.sourceforge.nethttp://wcollage.sourceforge.net
>
> --- On Fri, 11/14/08, Ben Schulz <[EMAIL PROTECTED]> wrote:
>
> > From: Ben Schulz <[EMAIL PROTECTED]>
> > Subject: [The Java Posse] Re: single-threaded vs concurrent performance on 
> > dual-core
> > To: "The Java Posse" <[email protected]>
> > Date: Friday, November 14, 2008, 7:00 PM
> > Playing one round is probably in the (low) 10E2 ticks, so in
> > relative
> > terms synchronization overhead of the thread pool is
> > probably pretty
> > high compared to the non existent synchronization overhead
> > of the
> > single threaded executor
>
> > With kind regards
> > Ben
>
> > On 14 Nov., 23:36, Alexey Zinger
> > <[EMAIL PROTECTED]> wrote:
> > > Sure, I realize how difficult it is to assess these
> > things, especially sight unseen.  I was mostly fishing to
> > see if there was a known issue with the concurrency API or
> > anything like that.
>
> > > But let me answer your questions and I'll post the
> > part that invokes concurrency as well.
>
> > > At the moment I don't have access to a Linux box,
> > but I'll see if I can get this stuff run on one.
>
> > > Not synchronizing on anything.  When I posted the
> > output, I was in fact using Math.random() API, which,
> > according to the JDK Javadocs, can synchronize, so I
> > switched to using java.util.Random.nextInt(int) off a
> > separate Random object, which is what the Javadoc tells you
> > to do if you're running in multiple threads.  That
> > actually hurt the single-threaded run by a few seconds and
> > didn't seem to affect the concurrent mode.
>
> > > I've run it in both server and client mode with
> > not perceptible difference.  I've also experimented
> > with different heap allocations, which seems mostly to
> > affect the concurrent mode by causing it to run out of heap
> > if not given enough memory.  Otherwise, there doesn't
> > seem to be much of a difference.
>
> > > As to the number of threads in concurrent mode,
> > I've experimented with different setups, but for now
> > I've settled on running each player as separate Future
> > objects, so that makes 3 threads in concurrent mode, and
> > each of those spawns up to separate Future objects for each
> > game play, limited to 1000 threads in concurrent mode.
>
> > > Here's the interesting part:
>
> > > package zinger.goats;
>
> > > import java.util.*;
> > > import java.util.concurrent.*;
>
> > > public class Play
> > > {
> > >         public static class Tester implements
> > Callable<String>
> > >         {
> > >                 protected final Player player;
> > >                 protected final int tries;
> > >                 protected final
> > ExecutorService executor;
>
> > >                 public Tester(final Player
> > player, final int tries, final ExecutorService executor)
> > >                 {
> > >                         this.player =
> > player;
> > >                         this.tries =
> > tries;
> > >                         this.executor =
> > executor;
> > >                 }
>
> > >                 public String call()
> > >                 {
> > >                         return
> > Play.testPlayer(this.player, this.tries, this.executor);
> > >                 }
> > >         }
>
> > >         public static void main(final String...
> > args)
> > >         {
> > >                 final long startMS =
> > System.currentTimeMillis();
> > >                 final int tries =
> > Integer.parseInt(args[0]);
> > >                 final int maxThreads =
> > args.length > 1 ? Integer.parseInt(args[1]) : 1;
> > >                 final ExecutorService
> > testExecutor = maxThreads > 1 ?  
> > >                        
> > Executors.newFixedThreadPool(Math.min(3, maxThreads)) :
> > >                        
> > Executors.newSingleThreadExecutor();
> > >                 final ExecutorService
> > gameExecutor = maxThreads > 1 ?
> > >                        
> > Executors.newFixedThreadPool(Math.min(maxThreads, tries)) :
> > >                        
> > Executors.newSingleThreadExecutor();
> > >                 try
> > >                 {
> > >                         final
> > List<Future<String>> futures = new
> > ArrayList<Future<String>>(3);
> > >                        
> > futures.add(testExecutor.submit(new Play.Tester(new
> > RandomPlayer(), tries, gameExecutor)));
> > >                        
> > futures.add(testExecutor.submit(new Play.Tester(new
> > NeverChangePlayer(), tries, gameExecutor)));
> > >                        
> > futures.add(testExecutor.submit(new Play.Tester(new
> > AlwaysChangePlayer(), tries, gameExecutor)));
> > >                        
> > for(Future<String> future : futures)
> > >                         {
> > >                                 try
> > >                                 {
> > >                                    
> >     System.out.println(future.get());
> > >                                 }
> > >                                
> > catch(final InterruptedException ex)
> > >                                 {
> > >                                    
> >     ex.printStackTrace();
> > >                                 }
> > >                                
> > catch(final ExecutionException ex)
> > >                                 {
> > >                                    
> >     ex.printStackTrace();
> > >                                 }
> > >                         }
> > >                 }
> > >                 finally
> > >                 {
> > >                        
> > System.out.println("Execution time: " +
> > ((System.currentTimeMillis() - startMS) / 1000L) +
> > "sec.");
> > >                        
> > testExecutor.shutdown();
> > >                        
> > gameExecutor.shutdown();
> > >                 }
> > >         }
>
> > >         public static String testPlayer(final
> > Player player, final int tries, final ExecutorService
> > executor)
> > >         {
> > >                 final StringBuilder sb = new
> > StringBuilder();
> > >                 int carCount = 0;
> > >                 int goatCount = 0;
> > >                 final int[] carDistribution =
> > new int[3];
> > >                 final int[] goatDistribution =
> > new int[3];
>
> > >                 final List<Game> games =
> > new ArrayList<Game>(tries);
> > >                 final
> > List<Future<?>> futures = new
> > ArrayList<Future<?>>(tries);
> > >                 for(int i = 0; i < tries;
> > ++i)
> > >                 {
> > >                         final Game game =
> > new Game(player, new Random());
> > >                         games.add(game);
> > >                        
> > futures.add(executor.submit(game));
> > >                 }
>
> > >                 for(int i = 0; i < tries;
> > ++i)
> > >                 {
> > >                         try
> > >                         {
> > >                                
> > futures.get(i).get();
> > >                         }
> > >                         catch(final
> > InterruptedException ex)
> > >                         {
> > >                                
> > ex.printStackTrace();
> > >                                
> > continue;
> > >                         }
> > >                         catch(final
> > ExecutionException ex)
> > >                         {
> > >                                
> > ex.printStackTrace();
> > >                                
> > continue;
> > >                         }
> > >                         final Game game =
> > games.get(i);
> > >                        
> > switch(game.getPrize())
> > >                         {
> > >                                 case
> > CAR:
> > >                                
> > ++carCount;
> > >                                 break;
>
> > >                                 case
> > GOAT:
> > >                                
> > ++goatCount;
> > >                                 break;
> > >                         }
> > >                         for(int doorIndex
> > = 0; doorIndex < game.doorView.size(); ++doorIndex)
> > >                         {
> > >                                 final
> > Game.Door door = game.doorView.get(doorIndex);
> > >                                
> > switch(door.open())
> > >                                 {
> > >                                    
> >     case CAR:
> > >                                    
> >     ++carDistribution[doorIndex];
> > >                                    
> >     break;
>
> > >                                    
> >     case GOAT:
> > >                                    
> >     ++goatDistribution[doorIndex];
> > >                                    
> >     break;
> > >                                 }
> > >                         }
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to