SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package przyklady03;
  2.  
  3. import java.util.function.IntBinaryOperator;
  4. import java.util.concurrent.BrokenBarrierException;
  5. import java.util.concurrent.CyclicBarrier;
  6.  
  7. public class SumyWierszamiSekwencyjnie {
  8.  
  9.     private static final int WIERSZE = 1000;
  10.     private static final int KOLUMNY = 1000;
  11.    
  12.     private static final CyclicBarrier bariera;
  13.     private static int[] sumy = new int[WIERSZE];
  14.  
  15.     static {
  16.         bariera = new CyclicBarrier(WIERSZE, new Runnable() {
  17.            
  18.             @Override
  19.             public void run() {
  20.                 for (int i = 0; i < WIERSZE; i++) {
  21.                     System.out.println(i + " " + sumy[i]);
  22.                 }
  23.             }
  24.         });
  25.     }    
  26.    
  27.    
  28.     private static class Worker implements Runnable {
  29.         private final int wiersz;
  30.         private final int kolumny;
  31.         private final IntBinaryOperator macierz;
  32.        
  33.        
  34.         public Worker(int wiersz, int kolumny, IntBinaryOperator macierz) {
  35.             this.wiersz = wiersz;
  36.             this.kolumny = kolumny;
  37.             this.macierz = macierz;
  38.         }
  39.        
  40.         @Override
  41.         public void run() {
  42.             try {
  43.                 int suma = 0;
  44.                 for (int k = 0; k < kolumny; ++k) {
  45.                     suma += macierz.applyAsInt(wiersz, k);
  46.                 }
  47.            
  48.                 sumy[wiersz] = suma;
  49.                 bariera.await();
  50.             } catch (InterruptedException | BrokenBarrierException e) {
  51.                 System.out.println(wiersz + " przerwany");
  52.                    
  53.             }
  54.            
  55.         }
  56.     }
  57.  
  58.     private static void piszSumyWierszy(int wiersze, int kolumny, IntBinaryOperator macierz) {
  59.         for (int w = 0; w < wiersze; ++w) {
  60.             new Thread(new Worker(w, kolumny, macierz)).start();
  61.         }
  62.     }
  63.  
  64.     public static void main(String[] args) {
  65.         piszSumyWierszy(WIERSZE, KOLUMNY, (wiersz, kolumna) -> {
  66.             int a = 2 * kolumna + 1;
  67.             return (wiersz + 1) * (a % 4 - 2) * a;
  68.         });
  69.     }
  70.  
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top