SHARE
TWEET

Java Executor Service

a guest Nov 18th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.concurrent.Callable;
  4. import java.util.concurrent.ExecutionException;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.Future;
  8.  
  9. public class ParallelClass {
  10.    
  11.     public static class PrimeResult {
  12.         private final long number;
  13.         private final boolean isPrime;
  14.         public PrimeResult (long number, boolean isPrime) {
  15.             this.number = number;
  16.             this.isPrime = isPrime;
  17.         }
  18.     }
  19.    
  20.    
  21.     // Method used to check if a number is a prime number
  22.     public static PrimeResult CheckIfPrime(long n) throws InterruptedException
  23.     {
  24.         long i;
  25.         for (i = 2; i <= n - 1; i++)
  26.         {
  27.             if (n % i == 0)
  28.             {
  29.                 return new PrimeResult(n, false);
  30.             }
  31.         }
  32.        
  33.         return new PrimeResult(n, true);
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.        
  38.         long first = 0;
  39.         long last  = 1_000_000;
  40.         int availableProcessors = Runtime.getRuntime().availableProcessors();
  41.        
  42.         System.out.println("Available processors: " + availableProcessors);
  43.         System.out.println("Starting check from " + first + " to " + last + "...");
  44.        
  45.         // Create a list of tasks to execute
  46.         List<Callable<PrimeResult>> tasks = new ArrayList<Callable<PrimeResult>>();
  47.         for (long i = first; i < last; i++) {
  48.            
  49.             final long iter = i;
  50.             Callable<PrimeResult> c = new Callable<PrimeResult>() {
  51.                 @Override
  52.                 public PrimeResult call() throws Exception {
  53.                     return CheckIfPrime(iter);
  54.                 }
  55.             };
  56.             tasks.add(c);
  57.         }
  58.        
  59.         // Create the Executor Service
  60.         ExecutorService exec = Executors.newFixedThreadPool(availableProcessors);
  61.         //ExecutorService exec = Executors.newSingleThreadExecutor();
  62.         try {
  63.             long start = System.currentTimeMillis();
  64.             List<Future<PrimeResult>> results = exec.invokeAll(tasks);
  65.             for (Future<PrimeResult> fpr : results) {
  66.                 System.out.println("Number " + fpr.get().number + " is prime? " + fpr.get().isPrime);
  67.             }
  68.             long elapsed = System.currentTimeMillis() - start;
  69.             System.out.println(String.format("Elapsed time: %d ms", elapsed));
  70.         } catch (Exception ex) {
  71.             System.out.println("An exception occurred! Exception message:");
  72.             System.out.println(ex.getMessage());
  73.         }
  74.         finally {
  75.             exec.shutdown();
  76.         }
  77.        
  78.         System.out.println("Finished!");
  79.     }
  80.  
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top