Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. public class MyPrimeTest {
  2.  
  3.     public static void main(String[] args) throws InterruptedException {
  4.         // TODO Auto-generated method stub
  5.         if (args.length < 3) {
  6.             System.out.println("Usage: MyPrimeTest numThread low high \n");
  7.             return;
  8.         }
  9.         int nthreads = Integer.parseInt(args[0]);
  10.         int low = Integer.parseInt(args[1]);
  11.         int high = Integer.parseInt(args[2]);
  12.         Counter c = new Counter();
  13.        
  14.         //test cost of serial code
  15.         long start = System.currentTimeMillis();
  16.         int numPrimeSerial = SerialPrime.numSerailPrimes(low, high);
  17.         long end = System.currentTimeMillis();
  18.         long timeCostSer = end - start;
  19.         System.out.println("Time cost of serial code: " + timeCostSer + " ms.");
  20.        
  21.         //test of concurrent code
  22.         // **************************************
  23.         // Write me here
  24.         Thread[] list = new Thread[nthreads];
  25.         for(int i = 0; i < nthreads; i++)
  26.         {
  27.             list[i] = new Thread();  
  28.         }
  29.        
  30.         long parrStart = System.currentTimeMillis();
  31.  
  32.         int step = (high - low) / nthreads;
  33.         Thread t;
  34.        
  35.         for(int i = 0; i < nthreads; i++)
  36.         {
  37.             list[i] = new Thread(new ThreadPrime(low,low+step, c));
  38.            low += step;
  39.            list[i].start();
  40.         }
  41.         for(int i = 0; i < nthreads; i++)
  42.         {
  43.             list[i].join();
  44.         }
  45.  
  46.                
  47.         long parrEnd = System.currentTimeMillis();
  48.        
  49.          long timeCostCon = parrEnd - parrStart;
  50.        
  51.         // **************************************
  52.         System.out.println("Time cost of parallel code: " + timeCostCon + " ms.");
  53.         System.out.format("The speedup ration is by using concurrent programming: %5.2f. %n", (double)timeCostSer / timeCostCon);
  54.        
  55.         System.out.println("Number prime found by serial code is: " + numPrimeSerial);
  56.         System.out.println("Number prime found by parallel code is " + c.total());
  57.     }
  58.        
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement