Advertisement
jaVer404

level16.lesson10.task05

Sep 12th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package com.javarush.test.level16.lesson10.task05;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /* Один для всех, все - для одного
  7. 1. Разберись, как работает программа.
  8. 1.1. Обрати внимание, что объект Water - один для всех нитей.
  9.  
  10. 2. Реализуй метод ourInterruptMethod, чтобы он прерывал все нити из threads.
  11. 3. В методе run исправь значения переменных:
  12.  
  13. 3.1. isCurrentThreadInterrupted - должна равняться значению метода isInterrupted у текущей нити.
  14. 3.2. threadName - должна равняться значению метода getName (реализовано в классе Thread) у текущей нити.
  15. */
  16.  
  17. public class Solution {
  18.     public static byte countThreads = 3;
  19.     static List<Thread> threads = new ArrayList<Thread>(countThreads);
  20.  
  21.     public static void main(String[] args) throws InterruptedException {
  22.         initThreadsAndStart();
  23.         Thread.sleep(3000);//Главная спит
  24.         ourInterruptMethod();
  25.  
  26.     }
  27.  
  28.     public static void ourInterruptMethod() {
  29.         for (int i = 0; i < countThreads; i++) {
  30.             threads.get(i).interrupt();
  31.         }
  32.  
  33.     }
  34.  
  35. /*--------------------------------------------------------*/
  36.     private static void initThreadsAndStart() {
  37.         Water water = new Water("water");
  38.         for (int i = 0; i < countThreads; i++) {
  39.             threads.add(new Thread(water, "#" + i));
  40.         }
  41.  
  42.         for (int i = 0; i < countThreads; i++) {
  43.             threads.get(i).start();
  44.         }
  45.     }
  46. /*--------------------------------------------------------*/
  47.  
  48.     public static class Water implements Runnable {
  49.         private String commonResource;
  50.  
  51.         public Water(String commonResource) {
  52.             this.commonResource = commonResource;
  53.         }
  54. /*--------------------------------------------------------*/
  55.         public void run() {
  56.             //fix 2 variables - исправь 2 переменных
  57.             boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted();
  58.             String threadName = Thread.currentThread().getName();
  59.  
  60.             try {
  61.                 while (!isCurrentThreadInterrupted) {
  62.  
  63.                     System.out.println("Объект " + commonResource + ", нить " + threadName);
  64.                     Thread.sleep(1000);
  65.                 }
  66.             } catch (InterruptedException e) {
  67.             }
  68.         }
  69. /*--------------------------------------------------------*/
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement