In the implementation, the distinction will come from the partitionning of the problem, the definitions of workers and forman, and the recombination of sub solutions.
In this project, you'll find implemenations including Thread, Runnable, Callable, Future and ExecutorService, being applyed to the same problem.
The project's workers (called counters) are PrimesCounterThread, PrimesCounterRunnable and PrimesCounterCallable. While PrimesCounterThread extends the Thread class, others implements Runnable and Callable interfaces, respectively. Each counter is intented to calculate the number of primes in the given part of a specific range.
The project's workers (called counters) are PrimesCounterThread, PrimesCounterRunnable and PrimesCounterCallable. While PrimesCounterThread extends the Thread class, others implements Runnable and Callable interfaces, respectively. Each counter is intented to calculate the number of primes in the given part of a specific range. ParalleCounter classes are formans of the Project. Each ParallelCounter uses a group of Counters to calculate the overall number, either using Java ExecutorService or more conventinal means to manage the muli-threading process.
This Java project has a hierarchical structure with three main packages: worker, counters, and parallelcounters. Let's break down the hierarchy:
1. worker Package:
- Worker Class: An abstract class representing a generic worker with a start and finish range. Has two constructors, one taking start and finish parameters and the other taking a WorkPartitioner.Part instance.
Contains getter methods for start and finish.
- WorkPartitioner Class: A utility class for partitioning work into parts. Contains a static method partitions that partitions a range into parts based on the number of workers.
Has an inner class Part representing a partition with start and finish values.
2. counters Package:
- PrimesCounter Class: Extends the Worker class and represents a worker for counting prime numbers. Contains a cnt variable to store the count of prime numbers. Implements the isPrime method to check if a number is prime.
- PrimesCounterCallable Class: Extends PrimesCounter and implements the Callable interface. Overrides the call method to count primes number within a range.
- PrimesCounterRunnable Class: Extends PrimesCounter and implements the Runnable interface. Overrides the run method to count primes number within a range. Contains methods startThread and joinThread to manage the execution of the runnable.
- PrimesCounterThread Class: Extends Thread and represents a thread for counting primes number within a range. Contains a constructor that takes start and finish parameters or a WorkPartitioner.Part instance. Overrides the run method to count primes number within a range. Includes methods isPrime and getSum for checking primality and retrieving the count.
3. parallelcounters Package:
- ParallelCounter0 Class: Utilizes PrimesCounterThread to perform parallel counting. Splits the work into parts, creates threads for each part, starts them, and waits for their completion. Calculates and returns the sum of primes number.
- ParallelCounter1 Class: Utilizes PrimesCounterRunnable for parallel counting using threads. Similar to ParallelCounter0 but uses runnable instead of directly extending Thread.
- ParallelCounter2 Class: Utilizes PrimesCounterThread and ExecutorService for parallel counting. Creates a thread pool, assigns tasks to threads, and waits for their completion. Calculates and returns the sum of primes number.
- ParallelCounter3 Class: Utilizes PrimesCounterCallable and ExecutorService for parallel counting. Creates a thread pool, assigns tasks to threads, and waits for their completion using invokeAll.
Calculates and returns the sum of primes number.
- ParallelCounter4 Class: Similar to ParallelCounter3 but uses executor.submit to submit tasks and obtain Future objects for results. Calculates and returns the sum of primes number.
ParalleCounter classes are formans of the Project. Each ParallelCounter uses a group of Counters to calculate the overall number, either using Java ExecutorService or more conventinal means to manage the muli-threading process.