Commit d57be0bb authored by Lenovo's avatar Lenovo

third step

parent 8c848e4a
package org.example.parallelsummers;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.example.summers.SummerRunnable;
import org.example.summers.SummerThread;
import org.example.worker.WorkPartitioner;
public class ParallelSummer0{
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
*/
public static long sum(int[] data, int threadCount) {
List<SummerThread> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new SummerThread(data,part));
}
for (SummerThread summerThread : summers) {
summerThread.start();
}
for (SummerThread summerThread : summers) {
try {
summerThread.join();
} catch (InterruptedException e) {
System.err.println("Thread cannot join!");
}
}
long sum = 0;
for (SummerThread summerThread : summers) {
sum += summerThread.getSum();
}
System.out.println("summer0 " + sum);
return sum;
}
}
package org.example.parallelsummers;
import java.util.ArrayList;
import java.util.List;
import org.example.summers.SummerRunnable;
import org.example.summers.SummerThread;
import org.example.worker.WorkPartitioner;
public class ParallelSummer1 {
public static long sum(int[] data, int threadCount) {
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerRunnable which it's run methods return void,
so you have to use for loop to get the result.
*/
List<SummerRunnable> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new SummerRunnable(data,part));
}
for (SummerRunnable SummerRunnable : summers) {
SummerRunnable.startThread();
}
for (SummerRunnable SummerRunnable : summers) {
try {
SummerRunnable.joinThread();
} catch (InterruptedException e) {
System.err.println("Thread cannot join!");
}
}
long sum = 0;
for (SummerRunnable summerRunnable : summers) {
sum += summerRunnable.getSum();
}
System.out.println("summer1 " + sum);
return sum;
}
}
package org.example.parallelsummers;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import org.example.summers.SummerThread;
import org.example.worker.WorkPartitioner;
public class ParallelSummer2{
public static long sum(int[] data, int threadCount) {
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerThread which it's run methods return void,
so you have to use for loop to get the result.
*/
List<SummerThread> summers = new ArrayList<>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new SummerThread(data,part));
}
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
for (SummerThread summer: summers) {
executor.execute(summer);
}
executor.shutdown();
//Waiting for all the thread to finalize
while (!executor.isTerminated()) {
}
System.out.println("summer2 "+ summers.stream().mapToLong(SummerThread::getSum).sum());
return summers.stream().mapToLong(SummerThread::getSum).sum();
}
}
package org.example.parallelsummers;
import java.util.ArrayList;
import java.util.Arrays;
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 org.example.summers.SummerCallable;
import org.example.summers.SummerThread;
import org.example.worker.WorkPartitioner;
public class ParallelSummer3 {
public static long sum(int[] data, int threadCount) {
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerCallable which it's run methods return future,
so you have to use for loop to get the result.
*/
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<SummerCallable> summers = new ArrayList<SummerCallable>();
List<WorkPartitioner.Part> parts = WorkPartitioner.partitions(data.length, threadCount);
for (WorkPartitioner.Part part : parts) {
summers.add(new SummerCallable(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 sum = 0;
for (Future<Long> future : results) {
try {
sum += future.get();
} catch (InterruptedException | ExecutionException e) {
System.err.println("Cannot get the results from threads.");
return -2;
}
}
System.out.println("summer3 " + sum);
return sum;
}
}
package org.example.parallelsummers;
import java.util.ArrayList;
import java.util.Arrays;
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 org.example.summers.SummerCallable;
import org.example.summers.SummerThread;
import org.example.worker.WorkPartitioner;
public class ParallelSummer4{
public static long sum(int[] data, int threadCount) {
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerCallable which it's run methods return future,
so you have to use for loop to get the result.
*/
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 SummerCallable(data, part)));
}
executor.shutdown();
while (!executor.isTerminated()) {
}
long sum = 0;
for (Future<Long> future : results) {
try {
sum += future.get();
} catch (InterruptedException | ExecutionException e) {
System.err.println("Cannot get the results from threads.");
return -2;
}
}
System.out.println("summer4 " + sum);
return sum;
}
}
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