Commit bd36be5a authored by julanar.ali's avatar julanar.ali

writing comments

parent 4a6e9301
...@@ -4,7 +4,7 @@ import parallelSummers.*; ...@@ -4,7 +4,7 @@ import parallelSummers.*;
public class main { public class main {
public static void main(String[] args) { public static void main(String[] args) {
int size = 10; int size = 100;
int[] data = TestData.createData(size); int[] data = TestData.createData(size);
long counterResult = SequentialCounter.primesCount(data); long counterResult = SequentialCounter.primesCount(data);
int threadCount = 8; int threadCount = 8;
......
...@@ -14,16 +14,23 @@ import summers.SummerThread; ...@@ -14,16 +14,23 @@ import summers.SummerThread;
import worker.WorkPartitioner; import worker.WorkPartitioner;
import worker.WorkPartitioner.Part; import worker.WorkPartitioner.Part;
//this class calculates the number of primes in [0 , size-1] using CounterThread
public class ParallelCounter0{ public class ParallelCounter0{
public static long primesCount(int[] data, int threadCount) { public static long primesCount(int[] data, int threadCount) {
List<CounterThread> counters = new ArrayList<>(); List<CounterThread> counters = new ArrayList<>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
//assigning each part to counter thread
for (Part part : parts) { for (Part part : parts) {
counters.add(new CounterThread(data,part)); counters.add(new CounterThread(data,part));
} }
//starting threads to calculate the number of primes
for (CounterThread counterThread : counters) { for (CounterThread counterThread : counters) {
counterThread.start(); counterThread.start();
} }
...@@ -36,6 +43,7 @@ public class ParallelCounter0{ ...@@ -36,6 +43,7 @@ public class ParallelCounter0{
} }
} }
//defining a variable to hold the total number of primes
long count = 0; long count = 0;
for (CounterThread counterThread : counters) { for (CounterThread counterThread : counters) {
count += counterThread.getCount(); count += counterThread.getCount();
......
...@@ -10,15 +10,22 @@ import java.util.List; ...@@ -10,15 +10,22 @@ import java.util.List;
//this class calculates the number of primes in [0 , size-1] using CounterRunnable which implements the Runnable Interface
public class ParallelCounter1 { public class ParallelCounter1 {
public static long primesCount(int[] data, int threadCount) { public static long primesCount(int[] data, int threadCount) {
List<CounterRunnable> counters = new ArrayList<>(); List<CounterRunnable> counters = new ArrayList<>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
//assigning each part to counter runnable
for (WorkPartitioner.Part part : parts) { for (WorkPartitioner.Part part : parts) {
counters.add(new CounterRunnable(data,part)); counters.add(new CounterRunnable(data,part));
} }
//starting the threads
for (CounterRunnable counterRunnable : counters) { for (CounterRunnable counterRunnable : counters) {
counterRunnable.startThread(); counterRunnable.startThread();
} }
...@@ -31,6 +38,7 @@ public class ParallelCounter1 { ...@@ -31,6 +38,7 @@ public class ParallelCounter1 {
} }
} }
//defining a variable to hold the total number of primes
long count = 0; long count = 0;
for (CounterRunnable counterRunnable : counters) { for (CounterRunnable counterRunnable : counters) {
count += counterRunnable.getCount(); count += counterRunnable.getCount();
......
...@@ -12,26 +12,36 @@ import java.util.List; ...@@ -12,26 +12,36 @@ import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
//this class calculates the number of primes in [0 , size-1]
// using ExecutorService with fixed thread pool of threadCount threads, and with counter thread
public class ParallelCounter2{ public class ParallelCounter2{
public static long primesCount(int[] data, int threadCount) { public static long primesCount(int[] data, int threadCount) {
List<CounterThread> counters = new ArrayList<>(); List<CounterThread> counters = new ArrayList<>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
//assigning each part to counter thread
for (Part part : parts) { for (Part part : parts) {
counters.add(new CounterThread(data,part)); counters.add(new CounterThread(data,part));
} }
//defining an executor with fixed thread pool to make each thread calculates a number of primes in its own part
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount);
//executing the threads
for (CounterThread counter : counters) { for (CounterThread counter : counters) {
executor.execute(counter); executor.execute(counter);
} }
//waiting until the current thread finishing its work and then shutting it down
executor.shutdown(); executor.shutdown();
//Waiting for all the thread to finalize //Waiting for all the thread to finalize
while (!executor.isTerminated()) { while (!executor.isTerminated()) {
//System.out.println("Processing...."); //System.out.println("Processing....");
} }
//returning the total number of primes
return counters.stream().mapToLong(CounterThread::getCount).sum(); return counters.stream().mapToLong(CounterThread::getCount).sum();
} }
......
...@@ -12,29 +12,41 @@ import java.util.concurrent.Future; ...@@ -12,29 +12,41 @@ import java.util.concurrent.Future;
import worker.WorkPartitioner.Part; import worker.WorkPartitioner.Part;
//this class calculates the number of primes in [0 , size-1]
// using ExecutorService with fixed thread pool of threadCount threads,
// and with CounterCallable which implements the Callable interface, by invoking all callables
public class ParallelCounter3 { public class ParallelCounter3 {
public static long primesCount(int[] data, int threadCount) { public static long primesCount(int[] data, int threadCount) {
//creating a pool of #threadCount threads //creating a fixed pool of threadCount threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<CounterCallable> counters = new ArrayList<CounterCallable>(); List<CounterCallable> counters = new ArrayList<CounterCallable>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
//assigning each part to counter callable
for (Part part : parts) { for (Part part : parts) {
counters.add(new CounterCallable(data,part)); counters.add(new CounterCallable(data,part));
} }
//the result that returning from call function in callable is returned as a future
// because the submit function doesn't wait until the task completes, so the executor service can't return the
//callable directly, instead it returns the result type Future, which can be retrieved later
List<Future<Long>> results; List<Future<Long>> results;
try { try {
// the invokeAll function allows us to submit many callables at once, it accepts a collection of callable,
//and returning a list of futures
results = executor.invokeAll(counters); results = executor.invokeAll(counters);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.err.println("Cannot invoke the threads."); System.err.println("Cannot invoke the threads.");
return -1; return -1;
} }
//waiting until the current thread finishing its work and then shutting it down
executor.shutdown(); executor.shutdown();
long count = 0; long count = 0;
......
...@@ -11,7 +11,9 @@ import java.util.concurrent.ExecutorService; ...@@ -11,7 +11,9 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
//this class calculates the number of primes in [0 , size-1]
// using ExecutorService with fixed thread pool of threadCount threads,
// and with CounterCallable which implements the Callable interface, by using submit function
public class ParallelCounter4 { public class ParallelCounter4 {
...@@ -19,13 +21,21 @@ public class ParallelCounter4 { ...@@ -19,13 +21,21 @@ public class ParallelCounter4 {
//creating a pool of 5 threads //creating a pool of 5 threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount);
//the result that returning from call function in callable is returned as a type Future
// because the submit function doesn't wait until the task completes, so the executor service can't return the
//callable directly, instead it returns the result type Future, which can be retrieved later
List<Future<Long>> results = new ArrayList<Future<Long>>(); List<Future<Long>> results = new ArrayList<Future<Long>>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount); List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
//assigning each part to counter callable
for (Part part : parts) { for (Part part : parts) {
results.add(executor.submit(new CounterCallable(data, part))); results.add(executor.submit(new CounterCallable(data, part)));
} }
//waiting until the current thread finishing its work and then shutting it down
executor.shutdown(); executor.shutdown();
while (!executor.isTerminated()) { while (!executor.isTerminated()) {
...@@ -33,6 +43,7 @@ public class ParallelCounter4 { ...@@ -33,6 +43,7 @@ public class ParallelCounter4 {
} }
long count = 0; long count = 0;
for (Future<Long> future : results) { for (Future<Long> future : results) {
try { try {
count += future.get(); count += future.get();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment