Advertisement
jaVer404

level16.lesson13.home08

Sep 18th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package com.javarush.test.level16.lesson13.home08;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. /* Кто первый встал - того и тапки
  10. 1. Разберись, что делает программа.
  11.     1.1. Каждая нить должна читать с консоли слова.
  12.          Используйте готовый static BufferedReader reader.
  13. //---------------------------------------------------------
  14.     1.2. Используй static byte countReadStrings,
  15.          чтобы посчитать, сколько слов уже считано с консоли всеми нитями.
  16.  
  17. 2. Реализуйте логику метода run:
  18.     2.1. Пока нить не прервана (!isInterrupted) читайте с консоли слова и добавляйте их в поле List<String> result.
  19.     2.2. Используй countReadStrings для подсчета уже считанных с консоли слов.
  20.  
  21.     1. Сначала мы вводим число count
  22.     2. Потм мы стартуем нити
  23.         2.1 В нитях мы вводим слова.
  24.         2.2 Слова добавляются к List<String> result
  25.         2.2 после каждого слова к countReadStrings плюсуется 1.
  26.  
  27. */
  28.  
  29. public class Solution {
  30.     public static volatile byte countReadStrings;
  31.     public static volatile BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  32.  
  33.     public static void main(String[] args) throws IOException {
  34.         //read count of strings
  35.         int count = Integer.parseInt(reader.readLine());
  36.         //init threads
  37.         ReaderThread consolReader1 = new ReaderThread();
  38.         ReaderThread consolReader2 = new ReaderThread();
  39.         ReaderThread consolReader3 = new ReaderThread();
  40.  
  41.         while (count > countReadStrings) {
  42.  
  43.  
  44.         }
  45.  
  46.         consolReader1.interrupt();
  47.         consolReader2.interrupt();
  48.         consolReader3.interrupt();
  49.         System.out.println("#1:" + consolReader1);
  50.         System.out.println("#2:" + consolReader2);
  51.         System.out.println("#3:" + consolReader3);
  52.  
  53.         reader.close();
  54.     }
  55.  
  56.     public static class ReaderThread extends Thread {
  57.         private List<String> result = new ArrayList<String>();
  58.  
  59.         public ReaderThread() {
  60.             start();
  61.         }
  62.  
  63.         public void run()  {
  64.             //add your code here - добавьте код тут
  65.             while (!isInterrupted()) {
  66.                 try {
  67.                     this.result.add(reader.readLine());
  68.                     countReadStrings++;
  69.  
  70.                 } catch (Exception e) {
  71.                 }
  72.             }
  73.         }
  74.  
  75.         @Override
  76.         public String toString() {
  77.             String s = result.toString();
  78.             return s.substring(1, s.length()-1);
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement