Programming 2 with Java

Exercise: Prime Counter with Thread Pool

Prime Counter

  1. Implement a Callable class PrimeCounterTask whose constructor takes a minimum and a maximum number and whose call method returns the number of prime numbers in between using the utitlity class
  2. Implement the program PrimeCounter which
    • takes a minimum and a maximum number as well as a number of threads as command line arguments
    • creates a thread pool with the specified number of threads
    • splits the given range into subranges and submits tasks each of which counts the prime numbers in one subrange
    • displays the total number of found prime numbers and the required runtime
    > java PrimeCounter 1 10000000 4
    664579 prime numbers found in 4.859 seconds
    (The table on the Wikipedia page of the prime-counting function can be used to verify the results.)
  3. Run the program with different number of threads and compare the runtimes.