Advertisement
Guest User

Untitled

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