Guest User

Untitled

a guest
Jan 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. import java.lang.management.ManagementFactory;
  2. import java.lang.management.ThreadMXBean;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import java.util.concurrent.*;
  6. import java.util.concurrent.atomic.AtomicLong;
  7.  
  8. public class ThreadingStressTest {
  9.  
  10.     private static ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
  11.  
  12.     public static void main(String args[]) throws InterruptedException, ExecutionException {
  13.         new ThreadingStressTest();
  14.     }
  15.    
  16.     public ThreadingStressTest() throws InterruptedException, ExecutionException{
  17.  
  18.         int upperBound = 64*1024;
  19.         int chunkSize = 64;
  20.         int chunkCount = upperBound / chunkSize;
  21.         int upperThreadCount = 24;
  22.  
  23.         System.out.printf("Parallel search for all primes [3 - %d] on %d-core CPU "
  24.                 + "using %d chunks sized %d\n",
  25.                 upperBound,
  26.                 Runtime.getRuntime().availableProcessors(),
  27.                 chunkCount,
  28.                 chunkSize);
  29.        
  30.     for(int i = 0; i < 500; i++){
  31.             findPrimes(upperBound, chunkSize, 1);
  32.             findPrimes(upperBound, chunkSize, 2);
  33.             for(int threadCount = 4; threadCount <= upperThreadCount; threadCount+=4){
  34.                     findPrimes(upperBound, chunkSize, threadCount);
  35.             }
  36.     }
  37.     }
  38.    
  39.     private void findPrimes(int upperBound, int chunkSize, int threadCount) throws InterruptedException,
  40.             ExecutionException {
  41.         final long before = System.nanoTime();
  42.  
  43.         final List<Integer> primes = new ArrayList<Integer>(upperBound);
  44.         final AtomicLong cpuTime = new AtomicLong();
  45.         final ExecutorService pool = Executors.newFixedThreadPool(threadCount);
  46.         final List<Future<PrimeResult>> futureResults = new ArrayList<Future<PrimeResult>>(threadCount);
  47.  
  48.         int chunkCount = upperBound / chunkSize;
  49.        
  50.         for (int i = 0; i < chunkCount; i++) {
  51.             futureResults.add( pool.submit(new ComputeChunk(
  52.                     upperBound / chunkCount * i,
  53.                     upperBound / chunkCount))
  54.             );
  55.         }
  56.  
  57.         for (Future<PrimeResult> futureResult : futureResults) {
  58.             primes.addAll(futureResult.get().getPrimes());
  59.             cpuTime.addAndGet(futureResult.get().getCpuTime());
  60.         }
  61.        
  62.         pool.shutdown();
  63.  
  64.         long after = System.nanoTime();
  65.         double secondsElapsed = (after - before) / 1000000000.0d;
  66.         double normalized = cpuTime.get()/ 1000000000.0d;
  67.  
  68.         System.out.printf("%d primes found using %d thread(s) in %.1fsec realtime, %.1f cpu-time. Scaling %.2f.\n",
  69.                 primes.size(),
  70.                 threadCount,
  71.                 secondsElapsed,
  72.                 normalized,
  73.                 normalized/secondsElapsed
  74.         );
  75.     }
  76.  
  77.    
  78.     class ComputeChunk implements Callable<PrimeResult> {
  79.  
  80.         private int start, end;
  81.         private List<Integer> primes = new ArrayList<Integer>();
  82.  
  83.         public ComputeChunk(int start, int length) {
  84.             this.start = start;
  85.             this.end = length + start;
  86.         }
  87.  
  88.         @Override
  89.         public PrimeResult call() {
  90.             long cpuTime = tmx.getCurrentThreadCpuTime();
  91.             for (int i = start; i < end; i++) {
  92.                 if (isPrime(i)) {
  93.                     primes.add(i);
  94.                 }
  95.             }
  96.  
  97.             return new PrimeResult(primes, tmx.getCurrentThreadCpuTime() - cpuTime);
  98.         }
  99.     }    
  100.  
  101.  
  102.     private static boolean isPrime(int candidate) {
  103.  
  104.         // Eliminate even no.
  105.         if ((candidate & 1) == 0) {
  106.             return false;
  107.         }
  108.  
  109.         // Find the upper bound we need to test for
  110.         long ubound = (long) Math.sqrt(candidate) + 2;
  111.  
  112.         for (int i = 3; i < ubound; i += 2) {
  113.             if ((candidate % i) == 0) {
  114.                 return false;
  115.             }
  116.         }
  117.  
  118.         return true;
  119.     }
  120.  
  121.     private class PrimeResult{
  122.         private List<Integer> primes;
  123.         private long cpuTime;
  124.        
  125.         public PrimeResult(List<Integer> primes, long cpuTime){
  126.             this.primes = primes;
  127.             this.cpuTime = cpuTime;
  128.         }
  129.        
  130.         public List<Integer> getPrimes(){
  131.             return primes;
  132.         }
  133.        
  134.         public long getCpuTime(){
  135.             return cpuTime;
  136.         }
  137.        
  138.     }
  139.  
  140. }
Add Comment
Please, Sign In to add comment