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

writing comments

parent 4a6e9301
......@@ -4,7 +4,7 @@ import parallelSummers.*;
public class main {
public static void main(String[] args) {
int size = 10;
int size = 100;
int[] data = TestData.createData(size);
long counterResult = SequentialCounter.primesCount(data);
int threadCount = 8;
......
......@@ -14,16 +14,23 @@ import summers.SummerThread;
import worker.WorkPartitioner;
import worker.WorkPartitioner.Part;
//this class calculates the number of primes in [0 , size-1] using CounterThread
public class ParallelCounter0{
public static long primesCount(int[] data, int threadCount) {
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);
//assigning each part to counter thread
for (Part part : parts) {
counters.add(new CounterThread(data,part));
}
//starting threads to calculate the number of primes
for (CounterThread counterThread : counters) {
counterThread.start();
}
......@@ -36,6 +43,7 @@ public class ParallelCounter0{
}
}
//defining a variable to hold the total number of primes
long count = 0;
for (CounterThread counterThread : counters) {
count += counterThread.getCount();
......
......@@ -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 static long primesCount(int[] data, int threadCount) {
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);
//assigning each part to counter runnable
for (WorkPartitioner.Part part : parts) {
counters.add(new CounterRunnable(data,part));
}
//starting the threads
for (CounterRunnable counterRunnable : counters) {
counterRunnable.startThread();
}
......@@ -31,6 +38,7 @@ public class ParallelCounter1 {
}
}
//defining a variable to hold the total number of primes
long count = 0;
for (CounterRunnable counterRunnable : counters) {
count += counterRunnable.getCount();
......
......@@ -12,26 +12,36 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
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 static long primesCount(int[] data, int threadCount) {
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);
//assigning each part to counter thread
for (Part part : parts) {
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);
//executing the threads
for (CounterThread counter : counters) {
executor.execute(counter);
}
//waiting until the current thread finishing its work and then shutting it down
executor.shutdown();
//Waiting for all the thread to finalize
while (!executor.isTerminated()) {
//System.out.println("Processing....");
}
//returning the total number of primes
return counters.stream().mapToLong(CounterThread::getCount).sum();
}
......
......@@ -12,29 +12,41 @@ import java.util.concurrent.Future;
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 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);
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);
//assigning each part to counter callable
for (Part part : parts) {
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;
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);
} catch (InterruptedException e) {
System.err.println("Cannot invoke the threads.");
return -1;
}
//waiting until the current thread finishing its work and then shutting it down
executor.shutdown();
long count = 0;
......
......@@ -11,7 +11,9 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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 {
......@@ -19,13 +21,21 @@ public class ParallelCounter4 {
//creating a pool of 5 threads
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>>();
//partitioning the data to assign each part to a counter to calculate the number of primes
List<Part> parts = WorkPartitioner.partitions(data.length, threadCount);
//assigning each part to counter callable
for (Part part : parts) {
results.add(executor.submit(new CounterCallable(data, part)));
}
//waiting until the current thread finishing its work and then shutting it down
executor.shutdown();
while (!executor.isTerminated()) {
......@@ -33,6 +43,7 @@ public class ParallelCounter4 {
}
long count = 0;
for (Future<Long> future : results) {
try {
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