Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 KB | None | 0 0
  1. package com.kulman;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. public class ParallelCalculations implements ParallelCalculationsInterface {
  8.     private PointGeneratorInterface pointGenerator;
  9.     private int numberOfPoints;
  10.     private List<Thread> threads;
  11.     private double[] geometricCenter;
  12.     private int[][] histogram;
  13.     private int threadCount;
  14.     private boolean run;
  15.  
  16.     public static void main(String[] args) {
  17.         ParallelCalculationsInterface parallelCalculations = new ParallelCalculations();
  18.         parallelCalculations.setNumberOfThreads(4);
  19.         parallelCalculations.setPointGenerator(new PointGenerator());
  20.         parallelCalculations.start();
  21.  
  22.         try {
  23.             Thread.sleep(2000);
  24.         } catch (InterruptedException e) {
  25.             System.out.println(e.toString());
  26.         }
  27.  
  28.         parallelCalculations.suspendCalculations();
  29.         System.out.println(parallelCalculations.getGeometricCenter()[0] + " " + parallelCalculations.getGeometricCenter()[1]);
  30.         for (int[] x : parallelCalculations.getHistogram())
  31.         {
  32.             for (int y : x)
  33.             {
  34.                 System.out.print(y + " ");
  35.             }
  36.             System.out.println();
  37.         }
  38.         try {
  39.             Thread.sleep(3000);
  40.         } catch (InterruptedException e) {
  41.             System.out.println(e.toString());
  42.         }
  43.         parallelCalculations.continueCalculations();
  44.  
  45.         try {
  46.             Thread.sleep(3000);
  47.         } catch (InterruptedException e) {
  48.             System.out.println(e.toString());
  49.         }
  50.         parallelCalculations.suspendCalculations();
  51.         System.out.println(parallelCalculations.getGeometricCenter()[0] + " " + parallelCalculations.getGeometricCenter()[1]);
  52.         for (int[] x : parallelCalculations.getHistogram())
  53.         {
  54.             for (int y : x)
  55.             {
  56.                 System.out.print(y + " ");
  57.             }
  58.             System.out.println();
  59.         }
  60. //        parallelCalculations.continueCalculations();
  61.     }
  62.  
  63.     class myRunnable implements Runnable {
  64.         PointInterface point;
  65.         @Override
  66.         public void run() {
  67.             System.out.println("Hej tu " + Thread.currentThread().getName());
  68.                 while (run || !Thread.currentThread().isInterrupted()) {
  69.                     calculate1();
  70.                     calculate2();
  71.                     calculate3();
  72.                     calculate4();
  73.                     calculate5();
  74.                 }
  75.         }
  76.         private synchronized void calculate1() {
  77.             point = pointGenerator.getPoint();
  78.         }
  79.         private synchronized void calculate2() {
  80.             numberOfPoints++;
  81.         }
  82.         private synchronized void calculate3() {
  83.             histogram[point.getPositions()[0]][point.getPositions()[1]]++;
  84.         }
  85.         private synchronized void calculate4() {
  86.             geometricCenter[0] += point.getPositions()[0];
  87.         }
  88.         private synchronized void calculate5() {
  89.             geometricCenter[1] += point.getPositions()[1];
  90.         }
  91.     }
  92.  
  93.     public ParallelCalculations() {
  94.         this.numberOfPoints = 0;
  95.         this.geometricCenter = new double[2];
  96.         this.histogram = new int[PointInterface.MAX_POSITION+1][PointInterface.MAX_POSITION+1];
  97.     }
  98.  
  99.     @Override
  100.     public void setPointGenerator(PointGeneratorInterface generator) {
  101.         this.pointGenerator = generator;
  102.     }
  103.  
  104.     @Override
  105.     public void setNumberOfThreads(int threads) {
  106.         this.threadCount = threads;
  107.         this.threads = new ArrayList<>(threads);
  108.         for(int i = 0; i < threads; i++) {
  109.             this.threads.add(new Thread(new myRunnable()));
  110.         }
  111.     }
  112.  
  113.     @Override
  114.     public void start() {
  115.         run = true;
  116.         for(Thread thread : threads) {
  117.             thread.start();
  118.         }
  119.     }
  120.  
  121.     @Override
  122.     public void suspendCalculations() {
  123.         run = false;
  124.         for (Thread thread : threads) {
  125.                 thread.interrupt();
  126.         }
  127.         System.out.println("Suspending");
  128.     }
  129.  
  130.     @Override
  131.     public void continueCalculations() {
  132.         this.threads = new ArrayList<>(threadCount);
  133.         for(int i = 0; i < threadCount; i++) {
  134.             this.threads.add(new Thread(new myRunnable()));
  135.         }
  136.         start();
  137.     }
  138.  
  139.     @Override
  140.     public double[] getGeometricCenter() {
  141.         return new double[]{geometricCenter[0]/numberOfPoints,geometricCenter[1]/numberOfPoints};
  142.     }
  143.  
  144.     @Override
  145.     public int[][] getHistogram() {
  146.         return histogram;
  147.     }
  148. }
  149.  
  150. class PointGenerator implements PointGeneratorInterface {
  151.     class Point implements PointInterface {
  152.         private int[] positions;
  153.  
  154.         public Point(int x, int y) {
  155.             this.positions = new int[2];
  156.             this.positions[0] = x;
  157.             this.positions[1] = y;
  158.  
  159.         }
  160.  
  161.         @Override
  162.         public int[] getPositions() {
  163.             return this.positions;
  164.         }
  165.     }
  166.  
  167.     public PointGenerator() { }
  168.     private Random random = new Random();
  169.     @Override
  170.     public PointInterface getPoint() {
  171.         System.out.println("getPoint()");
  172.         return new Point(random.nextInt(PointInterface.MAX_POSITION + 1), random.nextInt(PointInterface.MAX_POSITION + 1));
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement