Guest User

Untitled

a guest
Jun 30th, 2011
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.util.concurrent.*;
  2.  
  3. class Test implements Runnable {
  4.    
  5.     static int numberOfThreads;
  6.     static CyclicBarrier barrier;
  7.    
  8.     public static void main(String[] args) throws InterruptedException {
  9.        
  10.         // TEST WITH 4 THREADS
  11.        
  12.         numberOfThreads = 4;
  13.         barrier = new CyclicBarrier(numberOfThreads);
  14.        
  15.         long start = System.currentTimeMillis();
  16.         Test t = new Test();
  17.         Thread[] threads = new Thread[numberOfThreads];
  18.         for (int i = 0; i < numberOfThreads; i++)
  19.             (threads[i] = new Thread(t)).start();
  20.        
  21.         for (Thread thread : threads)
  22.             thread.join();
  23.        
  24.         System.out.println("4 threads: " + (System.currentTimeMillis() - start));
  25.  
  26.    
  27.    
  28.         // TEST WITH 1 THREAD
  29.         numberOfThreads = 1;
  30.         barrier = new CyclicBarrier(numberOfThreads);
  31.        
  32.         start = System.currentTimeMillis();
  33.         t = new Test();
  34.         threads = new Thread[numberOfThreads];
  35.         for (int i = 0; i < numberOfThreads; i++)
  36.             (threads[i] = new Thread(t)).start();
  37.        
  38.         for (Thread thread : threads)
  39.             thread.join();
  40.        
  41.         System.out.println("1 thread: " + (System.currentTimeMillis() - start));
  42.  
  43.     }
  44.    
  45.    
  46.    
  47.    
  48.    
  49.     public void run() {
  50.  
  51.         int i = 0;
  52.        
  53.         while (i++ < 5) {
  54.             try {
  55.                
  56.                 // perform an ammount of computations inversely proportional to
  57.                 // the number of threads.
  58.                 for (int j = 0; j < 10000 / numberOfThreads; j++)
  59.                     countPrimes();
  60.                
  61.                 barrier.await(); // CyclicBarrier waiting for all threads
  62.             } catch (Exception e) {
  63.                 e.printStackTrace();
  64.             }
  65.         }
  66.     }
  67.    
  68.    
  69.     public void countPrimes() {
  70.        
  71.         int i = 0;
  72.         int primeNumberCounter = 0;
  73.        
  74.         while (++i <= 1000) {
  75.  
  76.             int i1 = (int) Math.ceil(Math.sqrt(i));
  77.            
  78.             boolean isPrimeNumber = false;
  79.  
  80.             while (i1 > 1) {
  81.  
  82.                 if ((i != i1) && (i % i1 == 0)) {
  83.                     isPrimeNumber = false;
  84.                     break;
  85.                 } else if (!isPrimeNumber) {
  86.                     isPrimeNumber = true;
  87.                 }
  88.  
  89.                 --i1;
  90.             }
  91.  
  92.             if (isPrimeNumber)
  93.                 ++primeNumberCounter;
  94.         }
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment