Commit ad2914d6 authored by yazan.halloul's avatar yazan.halloul

Add worker, Prime Counters and Parallel Counters commit

parent d800566d
package parallelCounters;
import java.util.ArrayList;
import java.util.List;
import primeCounters.PrimeCounterThread;
import worker.WorkPartitioner;
/**
* This class calculates the sum of an array using {@link PrimeCounterThread} and conventional thread
* start and join mechanisms.
*
* @author Ahmet Cengizhan Dirican
*/
public class ParallelCounter0 {
public static long findPrimeNumbers(int[] data, int threadCount) {
List<PrimeCounterThread> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new PrimeCounterThread(data,part));
}
for (PrimeCounterThread summerThread : summers) {
summerThread.start();
}
for (PrimeCounterThread summerThread : summers) {
try {
summerThread.join();
} catch (InterruptedException e) {
System.err.println("Thread cannot join!");
}
}
long numberOfPrimeNumbers = 0;
for (PrimeCounterThread summerThread : summers) {
// System.out.println(summerThread.getPrimeNumbers());
numberOfPrimeNumbers += summerThread.getPrimeNumbers();
}
return numberOfPrimeNumbers;
}
}
package parallelCounters;
import java.util.ArrayList;
import java.util.List;
import primeCounters.PrimeCounterRunnable;
import worker.WorkPartitioner;
/**
* This class calculates the sum of an array using {@link PrimeCounterRunnable} and conventional thread
* start and join mechanisms placed into {@link PrimeCounterRunnable}.
*
* @author Ahmet Cengizhan Dirican
*/
public class ParallelCounter1 {
public static long findPrimeNumbers(int[] data, int threadCount) {
List<PrimeCounterRunnable> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new PrimeCounterRunnable(data,part));
}
for (PrimeCounterRunnable SummerRunnable : summers) {
SummerRunnable.startThread();
}
for (PrimeCounterRunnable SummerRunnable : summers) {
try {
SummerRunnable.joinThread();
} catch (InterruptedException e) {
System.err.println("Thread cannot join!");
}
}
long numberOfPrimeNumbers = 0;
for (PrimeCounterRunnable summerRunnable : summers) {
numberOfPrimeNumbers += summerRunnable.getPrimeNumbers();
}
return numberOfPrimeNumbers;
}
}
package parallelCounters;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import primeCounters.PrimeCounterThread;
import worker.WorkPartitioner;
/**
* This class calculates the sum of an array using {@link PrimeCounterThread} and {@link ExecutorService}
* with a fixed thread pool. The class executes the threads one bye one.
*
* @author Ahmet Cengizhan Dirican
*/
public class ParallelCounter2 {
public static long findPrimeNumbers(int[] data, int threadCount) {
List<PrimeCounterThread> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new PrimeCounterThread(data,part));
}
//creating a pool of 5 threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
for (PrimeCounterThread summer: summers) {
executor.execute(summer);
}
executor.shutdown();
//Waiting for all the thread to finalize
while (!executor.isTerminated()) {
//System.out.println("Processing....");
}
return summers.stream().mapToLong(PrimeCounterThread::getPrimeNumbers).sum();
}
}
package parallelCounters;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import primeCounters.PrimeCounterCallable;
import worker.WorkPartitioner;
/**
* This class calculates the sum of an array using {@link PrimeCounterCallable} and {@link ExecutorService}
* with a fixed thread pool. The class invokes all the callables at once.
*
* @author Ahmet Cengizhan Dirican
*/
public class ParallelCounter3 {
public static long findPrimeNumbers(int[] data, int threadCount) {
//creating a pool of #threadCount threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<PrimeCounterCallable> summers = new ArrayList<PrimeCounterCallable>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new PrimeCounterCallable(data,part));
}
List<Future<Long>> results;
try {
results = executor.invokeAll(summers);
} catch (InterruptedException e) {
System.err.println("Cannot invoke the threads.");
return -1;
}
executor.shutdown();
long numberOfPrimeNumbers = 0;
for (Future<Long> future : results) {
try {
numberOfPrimeNumbers += future.get();
} catch (InterruptedException | ExecutionException e) {
System.err.println("Cannot get the results from threads.");
return -2;
}
}
return numberOfPrimeNumbers;
}
}
package parallelCounters;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import primeCounters.PrimeCounterCallable;
import primeCounters.PrimeCounterThread;
import worker.WorkPartitioner;
/**
* This class calculates the sum of an array using {@link PrimeCounterThread} and {@link ExecutorService}
* with a fixed thread pool. The class submits the threads one bye one for executions.
*
* @author Ahmet Cengizhan Dirican
*/
public class ParallelCounter4 {
public static long findPrimeNumbers(int[] data, int threadCount) {
//creating a pool of 5 threads
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<Future<Long>> results = new ArrayList<Future<Long>>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
results.add(executor.submit(new PrimeCounterCallable(data, part)));
}
executor.shutdown();
while (!executor.isTerminated()) {
//System.out.println("Processing...");
}
long numberOfPrimeNumbers = 0;
for (Future<Long> future : results) {
try {
numberOfPrimeNumbers += future.get();
} catch (InterruptedException | ExecutionException e) {
System.err.println("Cannot get the results from threads.");
return -2;
}
}
return numberOfPrimeNumbers;
}
}
\ No newline at end of file
package primeCounters;
import worker.WorkPartitioner.Part;
import worker.Worker;
/**
* Base summer/worker class to calculate the sum of an array.
*
* @author Ahmet Cengizhan Dirican
*/
public class PrimeCounter extends Worker {
protected long numberOfPrimeNumbers;
public PrimeCounter(int[] data, Part part) {
super(data, part);
}
public long getPrimeNumbers() {
return numberOfPrimeNumbers;
}
}
package primeCounters;
import java.util.concurrent.Callable;
import primeChecker.PrimeChecker;
import worker.WorkPartitioner.Part;
/**
* {@link Callable} based {@link PrimeCounter ).
*
* @author Ahmet Cengizhan Dirican
* @see https://github.com/acdirican
*/
public class PrimeCounterCallable extends PrimeCounter implements Callable<Long> {
PrimeChecker primeChecker = new PrimeChecker();
public PrimeCounterCallable(int[] data, Part part) {
super(data, part);
}
@Override
public Long call() throws Exception {
numberOfPrimeNumbers = 0;
for (int i = start; i < finish; i++) {
if(primeChecker.isPrime(data[i]))
numberOfPrimeNumbers += 1;
}
return numberOfPrimeNumbers;
}
}
\ No newline at end of file
package primeCounters;
import primeChecker.PrimeChecker;
import worker.WorkPartitioner.Part;
/**
* {@link Runnable} based {@link PrimeCounter ).
*
* @author Ahmet Cengizhan Dirican
* @see https://github.com/acdirican
*/
public class PrimeCounterRunnable extends PrimeCounter implements Runnable{
PrimeChecker primeChecker = new PrimeChecker();
private Thread thread;
public PrimeCounterRunnable(int[] data, Part part) {
super(data, part);
}
@Override
public void run() {
numberOfPrimeNumbers = 0;
for (int i = start; i < finish; i++) {
if(primeChecker.isPrime(data[i]))
numberOfPrimeNumbers += 1;
}
}
public void startThread() {
thread = new Thread(this);
thread.start();
}
public void joinThread() throws InterruptedException {
thread.join();
}
}
\ No newline at end of file
package primeCounters;
import primeChecker.PrimeChecker;
import worker.WorkPartitioner.Part;
/**
* {@link Thread} based summer/worker Because this class cannot extends the {@link PrimeCounter},
* it had to include the similar code.
*
* @author Ahmet Cengizhan Dirican
*/
public class PrimeCounterThread extends Thread{
PrimeChecker primeChecker = new PrimeChecker();
private int start;
private int finish;
private int[] data;
private long numberOfPrimeNumbers;
public PrimeCounterThread(int[] data, int start, int finish) {
this.start = start;
this.finish = finish;
this.data = data;
}
public PrimeCounterThread(int[] data, Part part) {
this(data, part.getStart(), part.getFinish());
}
@Override
public void run() {
numberOfPrimeNumbers = 0;
for (int i = start; i < finish; i++) {
if(primeChecker.isPrime(data[i]))
numberOfPrimeNumbers += 1;
}
}
public long getPrimeNumbers() {
return numberOfPrimeNumbers;
}
}
\ No newline at end of file
package worker;
import java.util.ArrayList;
import java.util.List;
/**
* This class paritition a given work into part. It a calculates the work parts start and finish indexes.
*
* @author Ahmet Cengizhan Dirican
*/
public class WorkPartitioner {
public static List<Part> partitions(int size, int workerCount){
List<Part> parts = new ArrayList<>();
int part = (int) Math.ceil( (double)size / workerCount);
for (int i = 0; i <workerCount ; i++) {
int start = i * part;
int finish = Math.min((i+1) * part, size);
parts.add(new Part(start, finish));
}
return parts;
}
public static class Part {
private final int start;
private final int finish;
public Part(int start, int finish) {
this.start = start;
this.finish = finish;
}
public int getStart() {
return start;
}
public int getFinish() {
return finish;
}
}
}
package worker;
import worker.WorkPartitioner.Part;
/**
* Base worker class for an array.
*
* @author Ahmet Cengizhan Dirican
*/
public abstract class Worker {
protected final int start;
protected final int finish;
protected final int[] data;
public Worker(int[] data, int start, int finish) {
this.start = start;
this.finish = finish;
this.data = data;
}
public Worker(int[] data, Part part) {
this(data, part.getStart(), part.getFinish());
}
public int getStart() {
return start;
}
public int getFinish() {
return finish;
}
public int[] getData() {
return data;
}
}
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