Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kulman;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class ParallelCalculations implements ParallelCalculationsInterface {
- private PointGeneratorInterface pointGenerator;
- private int numberOfPoints;
- private List<Thread> threads;
- private double[] geometricCenter;
- private int[][] histogram;
- private int threadCount;
- private boolean run;
- public static void main(String[] args) {
- ParallelCalculationsInterface parallelCalculations = new ParallelCalculations();
- parallelCalculations.setNumberOfThreads(4);
- parallelCalculations.setPointGenerator(new PointGenerator());
- parallelCalculations.start();
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- System.out.println(e.toString());
- }
- parallelCalculations.suspendCalculations();
- System.out.println(parallelCalculations.getGeometricCenter()[0] + " " + parallelCalculations.getGeometricCenter()[1]);
- for (int[] x : parallelCalculations.getHistogram())
- {
- for (int y : x)
- {
- System.out.print(y + " ");
- }
- System.out.println();
- }
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- System.out.println(e.toString());
- }
- parallelCalculations.continueCalculations();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- System.out.println(e.toString());
- }
- parallelCalculations.suspendCalculations();
- System.out.println(parallelCalculations.getGeometricCenter()[0] + " " + parallelCalculations.getGeometricCenter()[1]);
- for (int[] x : parallelCalculations.getHistogram())
- {
- for (int y : x)
- {
- System.out.print(y + " ");
- }
- System.out.println();
- }
- // parallelCalculations.continueCalculations();
- }
- class myRunnable implements Runnable {
- PointInterface point;
- @Override
- public void run() {
- System.out.println("Hej tu " + Thread.currentThread().getName());
- while (run || !Thread.currentThread().isInterrupted()) {
- calculate1();
- calculate2();
- calculate3();
- calculate4();
- calculate5();
- }
- }
- private synchronized void calculate1() {
- point = pointGenerator.getPoint();
- }
- private synchronized void calculate2() {
- numberOfPoints++;
- }
- private synchronized void calculate3() {
- histogram[point.getPositions()[0]][point.getPositions()[1]]++;
- }
- private synchronized void calculate4() {
- geometricCenter[0] += point.getPositions()[0];
- }
- private synchronized void calculate5() {
- geometricCenter[1] += point.getPositions()[1];
- }
- }
- public ParallelCalculations() {
- this.numberOfPoints = 0;
- this.geometricCenter = new double[2];
- this.histogram = new int[PointInterface.MAX_POSITION+1][PointInterface.MAX_POSITION+1];
- }
- @Override
- public void setPointGenerator(PointGeneratorInterface generator) {
- this.pointGenerator = generator;
- }
- @Override
- public void setNumberOfThreads(int threads) {
- this.threadCount = threads;
- this.threads = new ArrayList<>(threads);
- for(int i = 0; i < threads; i++) {
- this.threads.add(new Thread(new myRunnable()));
- }
- }
- @Override
- public void start() {
- run = true;
- for(Thread thread : threads) {
- thread.start();
- }
- }
- @Override
- public void suspendCalculations() {
- run = false;
- for (Thread thread : threads) {
- thread.interrupt();
- }
- System.out.println("Suspending");
- }
- @Override
- public void continueCalculations() {
- this.threads = new ArrayList<>(threadCount);
- for(int i = 0; i < threadCount; i++) {
- this.threads.add(new Thread(new myRunnable()));
- }
- start();
- }
- @Override
- public double[] getGeometricCenter() {
- return new double[]{geometricCenter[0]/numberOfPoints,geometricCenter[1]/numberOfPoints};
- }
- @Override
- public int[][] getHistogram() {
- return histogram;
- }
- }
- class PointGenerator implements PointGeneratorInterface {
- class Point implements PointInterface {
- private int[] positions;
- public Point(int x, int y) {
- this.positions = new int[2];
- this.positions[0] = x;
- this.positions[1] = y;
- }
- @Override
- public int[] getPositions() {
- return this.positions;
- }
- }
- public PointGenerator() { }
- private Random random = new Random();
- @Override
- public PointInterface getPoint() {
- System.out.println("getPoint()");
- return new Point(random.nextInt(PointInterface.MAX_POSITION + 1), random.nextInt(PointInterface.MAX_POSITION + 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement