Advertisement
Guest User

Array Read - Thread Blocking test

a guest
Feb 15th, 2013
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.util.concurrent.Executors;
  2. import java.util.concurrent.ThreadPoolExecutor;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. import javax.swing.SwingWorker;
  6.  
  7.  
  8.  
  9.  
  10. public class TestField {
  11.    
  12.    
  13.     public static void main(String[] args) {
  14.        
  15.         //Storing stuff, obvious stuff
  16.         int numIterations = 5000;
  17.         double singleTimeSum = 0, multiTimeSum = 0;
  18.        
  19.         //hasPrint and anything to do with it are simply a convenience thing, for print out the percentage complete every 10%
  20.         boolean hasPrint = true;
  21.        
  22.         for(int i=0; i<numIterations; i++) {
  23.             //Again, these two lines are just convenience, they print out x% every 10 percent
  24.             if(!hasPrint && ((i*100)/numIterations) % 10 == 0) System.out.println((i*100)/numIterations + "%");
  25.             hasPrint = ((i*100)/numIterations) % 10 == 0;
  26.  
  27.             //Run test
  28.             new Go();
  29.            
  30.            
  31.             try {
  32.                 //We gotta hold up while our threads properly die, otherwise it makes them faster than it can destroy them,
  33.                 //and computer is SAD
  34.                 Thread.sleep(10);
  35.             } catch (InterruptedException e) {
  36.                 e.printStackTrace();
  37.             }
  38.            
  39.             //Add time to sum
  40.             singleTimeSum += singleTime;
  41.             multiTimeSum += multiTime;
  42.            
  43.             //and reset times
  44.             singleTime = 0;
  45.             multiTime = 0;
  46.         }
  47.        
  48.         //Print out results
  49.         System.out.println("Results (across " + numIterations + " tests): ");
  50.         System.out.println("Single-Thread time:\t" + (singleTimeSum / (float)numIterations));
  51.         System.out.println("Multi-Thread time:\t" + (multiTimeSum / (float)numIterations));
  52.     }
  53.  
  54.    
  55.     //Go references these variables, as though "out" parameters from a class.
  56.     static double multiTime, singleTime;
  57.    
  58.     //Runs the test once, stores results in multiTime, singleTime
  59.     static class Go {
  60.         public Go() {
  61.            
  62.             //Fill an array of 20k random doubles, just, because.
  63.             final double[] array = new double[20000];
  64.             for (int i = 0; i < array.length; i++) array[i] = Math.random();
  65.            
  66.             //The two threads store their results in here
  67.             final double[] threadTime = new double[2];
  68.            
  69.             //This is the worker thread
  70.             SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
  71.  
  72.                 @Override
  73.                 protected Void doInBackground() throws Exception {
  74.                     double temp;
  75.                    
  76.                     //For timing, start clock
  77.                     long localTime = System.nanoTime();
  78.                    
  79.                     //read from the array start-to-end, 6000 times
  80.                     for(int readIndex = 0; readIndex < 6000; readIndex++) {
  81.                         for (int i = 0; i < array.length; i++) {
  82.                             temp = array[i];
  83.                         }
  84.                     }
  85.                    
  86.                     //Store end time now, so that we may perform processign with this value without overhead
  87.                     long endTime = System.nanoTime();
  88.                    
  89.                     //Store in our array, without knowing which thread will end first
  90.                     if(threadTime[0] < 0) threadTime[0] = (endTime - localTime)/1e6;
  91.                     else threadTime[1] = (endTime - localTime)/1e6;
  92.                     return null;
  93.                 }
  94.                
  95.             };
  96.  
  97.             //This is an exact copy of the worker thread, I was unsure if just executing the SAME instance of it would execute as expected
  98.             SwingWorker<Void, Void> worker2 = new SwingWorker<Void, Void>() {
  99.                
  100.                 @Override
  101.                 protected Void doInBackground() throws Exception {
  102.                     double temp;
  103.                    
  104.                     long localTime = System.nanoTime();
  105.                    
  106.                     for(int readIndex = 0; readIndex < 6000; readIndex++) {
  107.                         for (int i = 0; i < array.length; i++) {
  108.                             temp = array[i];
  109.                         }
  110.                     }
  111.                    
  112.                     long endTime = System.nanoTime();
  113.                    
  114.                     if(threadTime[0] < 0) threadTime[0] = (endTime - localTime)/1e6;
  115.                     else threadTime[1] = (endTime - localTime)/1e6;
  116.                     return null;
  117.                 }
  118.                
  119.             };
  120.            
  121.            
  122.             //Thread pool
  123.             ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  124.            
  125.             //Execute each thread
  126.             pool.execute(worker);
  127.             pool.execute(worker2);
  128.            
  129.             try {
  130.                 //Shutdown and WAIT for our threads, so that we don't SPAM the computer with threads (it still does, anyway.)
  131.                 pool.shutdown();
  132.                 pool.awaitTermination(10000, TimeUnit.MILLISECONDS);
  133.             } catch (InterruptedException e) {
  134.                 e.printStackTrace();
  135.             }
  136.            
  137.             //Average the two Thread times.
  138.             multiTime += (threadTime[0] + threadTime[1]) / 2.0;
  139.            
  140.             //Basically, run the same code on the main thread.
  141.             //Could have made another worker to just run by itself, but, meh
  142.             double temp;
  143.             long startTime = System.nanoTime();
  144.             for(int r=0; r<6000; r++) {
  145.                 for (int i = 0; i < array.length; i++) {
  146.                     temp = array[i];
  147.                 }
  148.             }
  149.            
  150.             //Store single time
  151.             singleTime += (System.nanoTime() - startTime)/1e6;
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement