package zad3; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.*; public class SekwencyjnyV4 { private static final int N_WIERSZY = 10; private static final int N_KOLUMN = 100; private static List< BlockingQueue > resLists = Collections.synchronizedList(new ArrayList < BlockingQueue >(N_WIERSZY)); private static class helpThread extends Thread { private int nr_wiersza; private int nr_kolumny; public helpThread(int nr_kolumny) { this.nr_kolumny = nr_kolumny; this.nr_wiersza = 0; this.start(); } @Override public void run() { while (nr_wiersza < N_WIERSZY) { try { resLists.get(nr_wiersza).put(Macierz.wartość(nr_wiersza, nr_kolumny)); nr_wiersza++; } catch (Exception e) { e.printStackTrace(); } } } } private static class sumThread extends Thread { private int nr_wiersza; public sumThread(int nr_wiersza) { this.nr_wiersza = nr_wiersza; this.start(); } @Override public void run() { int res = 0; int got = 0; while (got!=N_KOLUMN) { try { res += resLists.get(nr_wiersza).take(); got++; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(nr_wiersza + " " + res); } } public static void main(String[] args) { for (int i = 0; i < N_WIERSZY; i++) { // initialization of lists BlockingQueue tmp = new LinkedBlockingQueue<>(); resLists.add(tmp); } for (int k = 0; k < N_KOLUMN; k++) { new helpThread(k); } for (int w = 0; w < N_WIERSZY; w++) { new sumThread(w); } } }