Advertisement
Guest User

Untitled

a guest
May 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) throws InterruptedException {
  6.         Letters letters = new Letters("ABCD"); // Konstruktor - podaję tutaj
  7.                                                 // dane z treści
  8.  
  9.         for (Thread t : letters.getThreads()) { // pętla for-each, przechodze po
  10.                                                 // wszystkim "odpalając" wątki,
  11.                                                 // tworze ciag
  12.             t.start();
  13.         }
  14.  
  15.         for (Thread t : letters.getThreads()) { // petla for-each, przechodze po
  16.                                                 // wszystkim tym razem
  17.                                                 // "nazywając" wątki
  18.             System.out.println(t.getName());
  19.         }
  20.  
  21.         Thread.sleep(5000); // usypiam wątki
  22.  
  23.         letters.print(); // wypisuje ciag
  24.         System.out.println("Program skończył działanie");
  25.  
  26.     }
  27.  
  28. }
  29.  
  30. class Letters {
  31.     ArrayList<Thread> threads = new ArrayList<Thread>(); // lista do
  32.                                                             // przechowywania
  33.                                                             // wątków
  34.  
  35.     /*
  36.      * Konstruktor klasy Latters, jako argument przyjmuje ciąg znaków (w
  37.      * przykładzie ABCD) potem iteruje po długości tworząc:
  38.      *
  39.      * a)
  40.      *
  41.      * Thread A Thread B ... Thread n
  42.      *
  43.      * b)
  44.      *
  45.      * Generuje ten ciąg (po każdej literze dodaje wygenerowany podciąg)
  46.      */
  47.     public Letters(String c) {
  48.         for (int i = 0; i < c.length(); i++) {
  49.             String letter = c.charAt(i) + "";
  50.             Runnable thread = () -> { // wyrażenie lambda - implementuje
  51.                                         // interfejs Runnable
  52.                 for (int j = 0; j < c.length(); j++) {
  53.                     try {
  54.                         // System.out.print(letter);
  55.                         total += letter;
  56.                         Thread.sleep(1000);
  57.                     } catch (InterruptedException e) { // w razie
  58.                                                         // nieprzewidzianej
  59.                                                         // ewentualności wyjątek
  60.                                                         // wypisze ze stosu kod
  61.                                                         // błedu
  62.                         e.printStackTrace();
  63.                         break;
  64.                     }
  65.                 }
  66.             };
  67.             threads.add(new Thread(thread, "Thread " + letter));
  68.  
  69.         }
  70.     }
  71.  
  72.     public ArrayList<Thread> getThreads() { // tak sobie biorę kolejne wątki do
  73.                                             // pracy
  74.         return threads;
  75.     }
  76.  
  77.     public void print() { // wypisuje na ekran wygenerowany ciąg
  78.         System.out.println(total);
  79.     }
  80.  
  81.     private String total = ""; // String do przechowywania generowanego ciągu -
  82.                                 // w przykładzie ACDACDBCDACD
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement