//Verily, verily, I say unto you, That ye shall weep and lament, but the world shall rejoice: //and ye shall be sorrowful, but your sorrow shall be turned into joy. (John 16:20) package com.javarush.task.task16.task1620; import java.util.ArrayList; import java.util.List; /* Один для всех, все - для одного */ public class Solution { public static byte countThreads = 3; static List threads = new ArrayList(countThreads); public static void main(String[] args) throws InterruptedException { initThreadsAndStart(); Thread.sleep(3000); ourInterruptMethod(); } public static void ourInterruptMethod() { for (int i = 0; i < countThreads; i++) { //add your code here - добавь код тут threads.get(i).interrupt(); } } private static void initThreadsAndStart() { Water water = new Water("water"); for (int i = 0; i < countThreads; i++) { threads.add(new Thread(water, "#" + i)); } for (int i = 0; i < countThreads; i++) { threads.get(i).start(); } } public static class Water implements Runnable { private String commonResource; public Water(String commonResource) { this.commonResource = commonResource; } public void run() { //fix 2 variables - исправь 2 переменных boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted(); String threadName = Thread.currentThread().getName(); try { while (!isCurrentThreadInterrupted) { System.out.println("Объект " + commonResource + ", нить " + threadName); Thread.sleep(1000); } } catch (InterruptedException e) { } } } } /* Один для всех, все - для одного 1. Разберись, как работает программа. 1.1. Обрати внимание, что объект Water — один для всех нитей. 2. Реализуй метод ourInterruptMethod, чтобы он прерывал все нити из threads. 3. В методе run исправь значения переменных: 3.1. isCurrentThreadInterrupted — должна равняться значению метода isInterrupted у текущей нити. 3.2. threadName — должна равняться значению метода getName (реализовано в классе Thread) у текущей нити. Требования: 1. Метод ourInterruptMethod должен прервать все нити из списка threads. 2. Метод run должен получать текущую нить с помощью Thread.currentThread. 3. Метод run должен использовать метод isInterrupted текущей нити. 4. Метод run должен использовать метод getName текущей нити. 5. Метод main должен работать примерно 3 сек. package com.javarush.task.task16.task1620; import java.util.ArrayList; import java.util.List; * Один для всех, все - для одного * public class Solution { public static byte countThreads = 3; static List threads = new ArrayList(countThreads); public static void main(String[] args) throws InterruptedException { initThreadsAndStart(); Thread.sleep(3000); ourInterruptMethod(); } public static void ourInterruptMethod() { //add your code here - добавь код тут } private static void initThreadsAndStart() { Water water = new Water("water"); for (int i = 0; i < countThreads; i++) { threads.add(new Thread(water, "#" + i)); } for (int i = 0; i < countThreads; i++) { threads.get(i).start(); } } public static class Water implements Runnable { private String commonResource; public Water(String commonResource) { this.commonResource = commonResource; } public void run() { //fix 2 variables - исправь 2 переменных boolean isCurrentThreadInterrupted = false; String threadName = ""; try { while (!isCurrentThreadInterrupted) { System.out.println("Объект " + commonResource + ", нить " + threadName); Thread.sleep(1000); } } catch (InterruptedException e) { } } } } */