После этого кода полагал что нить прервется
while (!Thread.currentThread().isInterrupted()) {
System.out.println(this);
if (--countdownIndex == 0) {
Thread.currentThread().interrupt();
Но она упорно продолжает выполняться дальше . Выводится в консоль нить прервана и пошло
#1: 0
#1: -1
#1: -2
#1: -3package com.javarush.task.task16.task1622;
/*
Последовательные выполнения нитей
*/
public class Solution {
public volatile static int COUNT = 4;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < COUNT; i++) {
new SleepingThread().join();
//напишите тут ваш код
}
}
public static class SleepingThread extends Thread {
private static volatile int threadCount = 0;
private volatile int countdownIndex = COUNT;
public SleepingThread() {
super(String.valueOf(++threadCount));
start();
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println(this);
if (--countdownIndex == 0) {
Thread.currentThread().interrupt();
try {
Thread.sleep(10);
}
catch(Exception e){
}
System.out.println("Нить прервана");//напишите тут ваш код
}
}
}
public String toString() {
return "#" + getName() + ": " + countdownIndex;
}
}
}