Advertisement
fiffeek

Untitled

Oct 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.concurrent.ConcurrentHashMap;
  3. import java.util.concurrent.CyclicBarrier;
  4. import java.util.concurrent.Semaphore;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6. import java.util.function.IntBinaryOperator;
  7.  
  8. public class semaphore_ex_v2 {
  9.  
  10.     private static final int WIERSZE = 10;
  11.     private static final int KOLUMNY = 100;
  12.     private static volatile AtomicInteger[] current_sum = new AtomicInteger[WIERSZE];
  13.     private static ConcurrentHashMap<Integer, AtomicInteger> hm = new ConcurrentHashMap<>();
  14.     private static ConcurrentHashMap<Integer, AtomicInteger> ai = new ConcurrentHashMap<>();
  15.     private static int actual_to_write = 0;
  16.     private static Semaphore mutex = new Semaphore(1);
  17.  
  18.     private static class threadRun implements Runnable {
  19.  
  20.         private int wiersz, kolumna;
  21.         private IntBinaryOperator macierz;
  22.  
  23.         threadRun(int wiersz, int kolumna, IntBinaryOperator macierz) {
  24.             this.wiersz = wiersz;
  25.             this.kolumna = kolumna;
  26.             this.macierz = macierz;
  27.         }
  28.  
  29.         @Override
  30.         public void run() {
  31.             while (this.wiersz < WIERSZE) {
  32.                 try {
  33.                     hm.get(this.wiersz).getAndAdd(macierz.applyAsInt(this.wiersz, this.kolumna));
  34.                     ai.get(this.wiersz).getAndDecrement();
  35.  
  36.                     mutex.acquire();
  37.                     if (actual_to_write == this.wiersz && ai.get(this.wiersz).get() == 0) {
  38.                         System.out.println(hm.get(this.wiersz).get());
  39.                         hm.remove(this.wiersz);
  40.                         ai.remove(this.wiersz);
  41.                         actual_to_write++;
  42.                     }
  43.                     mutex.release();
  44.  
  45.                     this.wiersz++;
  46.                 } catch (Exception e) {
  47.  
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     private static void piszSumyWierszy(int wiersze, int kolumny, IntBinaryOperator macierz) {
  54.  
  55.         for (int i = 0; i < WIERSZE; ++i) {
  56.             hm.put(i, new AtomicInteger(0));
  57.             ai.put(i, new AtomicInteger(KOLUMNY));
  58.         }
  59.  
  60.         ArrayList<Thread> al = new ArrayList<>();
  61.  
  62.         for (int k = 0; k < kolumny; ++k) {
  63.             Thread t = new Thread(new threadRun(0, k, macierz));
  64.             al.add(t);
  65.             t.start();
  66.         }
  67.  
  68.         for (Thread t: al) {
  69.             try {
  70.                 t.join();
  71.             } catch (Exception e) {
  72.  
  73.             }
  74.         }
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         piszSumyWierszy(WIERSZE, KOLUMNY, (wiersz, kolumna) -> {
  79.             int a = 2 * kolumna + 1;
  80.             return (wiersz + 1) * (a % 4 - 2) * a;
  81.         });
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement