//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) {
}
}
}
}
*/