Facebook
From Walloping Pintail, 8 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 418
  1. package zad3;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.concurrent.*;
  7.  
  8. public class SekwencyjnyV4 {
  9.  
  10.         private static final int N_WIERSZY = 10;
  11.     private static final int N_KOLUMN = 100;
  12.     private static List< BlockingQueue <Integer> > resLists =
  13.         Collections.synchronizedList(new ArrayList < BlockingQueue <Integer>>(N_WIERSZY));
  14.  
  15.     private static class helpThread extends Thread {
  16.         private int nr_wiersza;
  17.         private int nr_kolumny;
  18.        
  19.         public helpThread(int nr_kolumny) {
  20.                 this.nr_kolumny = nr_kolumny;
  21.             this.nr_wiersza = 0;
  22.                 this.start();
  23.         }
  24.  
  25.         @Override
  26.         public void run() {
  27.                 while (nr_wiersza < N_WIERSZY) {
  28.                         try {
  29.                                
  30.                                 resLists.get(nr_wiersza).put(Macierz.wartość(nr_wiersza, nr_kolumny));
  31.                     nr_wiersza++;
  32.                         } catch (Exception e) {
  33.                                 e.printStackTrace();
  34.                         }
  35.                     }
  36.         }
  37.     }
  38.  
  39.     private static class sumThread extends Thread {
  40.         private int nr_wiersza;
  41.  
  42.         public sumThread(int nr_wiersza) {
  43.             this.nr_wiersza = nr_wiersza;
  44.             this.start();
  45.         }
  46.  
  47.         @Override
  48.         public void run() {
  49.                 int res = 0;
  50.                 int got = 0;
  51.                 while (got!=N_KOLUMN)
  52.                 {
  53.                         try {
  54.                                         res += resLists.get(nr_wiersza).take();
  55.                                         got++;
  56.                                 } catch (InterruptedException e) {
  57.                                         // TODO Auto-generated catch block
  58.                                         e.printStackTrace();
  59.                                 }
  60.                 }
  61.                 System.out.println(nr_wiersza + " " + res);
  62.             }
  63.     }
  64.  
  65.     public static void main(String[] args) {
  66.         for (int i = 0; i < N_WIERSZY; i++) {
  67.             // initialization of lists
  68.             BlockingQueue<Integer> tmp = new LinkedBlockingQueue<>();
  69.             resLists.add(tmp);
  70.  
  71.         }
  72.  
  73.         for (int k = 0; k < N_KOLUMN; k++) {
  74.            new helpThread(k);
  75.         }
  76.         for (int w = 0; w < N_WIERSZY; w++) {
  77.             new sumThread(w);
  78.         }
  79.     }
  80. }