Приветствую, начал решать задачу, и вдруг понял, что не понимаю, почему значение в условии (51 строка) может поменяться: я ведь делаю start у каждой нити один раз, следовательно инициализация значения переменной isCurrentThreadInterrupted (47 строка) происходит тоже один раз, следовательно цикл должен быть бесконечным, но он прерывается (из за interrupt), и я не могу понять почему значение переменной isCurrentThreadInterrupted меняется, если второй раз start не запущен, а следовательно вторичная инициализация не происходит.
package com.javarush.task.task16.task1620;
import java.util.ArrayList;
import java.util.List;
/*
Один для всех, все - для одного
*/
public class Solution {
public static byte threadCount = 3;
static List<Thread> threads = new ArrayList<Thread>(threadCount);
public static void main(String[] args) throws InterruptedException {
initThreadsAndStart();
Thread.sleep(3000);
ourInterruptMethod();
}
public static void ourInterruptMethod() {
for (Thread thread: threads
) {
thread.interrupt();
}
}
private static void initThreadsAndStart() {
Water water = new Water("water");
for (int i = 0; i < threadCount; i++) {
threads.add(new Thread(water, "#" + i));
}
for (int i = 0; i < threadCount; i++) {
threads.get(i).start();
}
}
public static class Water implements Runnable {
private String sharedResource;
public Water(String sharedResource) {
this.sharedResource = sharedResource;
}
public void run() {
//fix 2 variables - исправь 2 переменных
boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted();
String threadName = Thread.currentThread().getName();
try {
while (!isCurrentThreadInterrupted) {
System.out.println("Объект " + sharedResource + ", нить " + threadName);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
}
}