Given code:

public class Solution { public static byte countThreads = 3; static List<Thread> threads = new ArrayList<Thread>(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++) { threads.get(i).interrupt(); } //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 = Thread.currentThread().interrupted(); String threadName = Thread.currentThread().getName(); try { while (!isCurrentThreadInterrupted) { System.out.println("Объект " + commonResource + ", нить " + threadName); Thread.sleep(1000); } } catch (InterruptedException e) { } } } } 

Question: why when the auxiliary thread called the interop ALREADY initialized variable isCurrentThreadInterrupted changed its value AFTER initial initialization ( boolean isCurrentThreadInterrupted = Thread.currentThread().interrupted(); )?

  • In the wile loop, the value of the isCurrentThreadInterrupted variable changes its value after the interrupt call on this thread - user7351520

1 answer 1

Nothing changes. You can see for yourself by running this code.

 import java.util.ArrayList; import java.util.List; public class Solution { public static byte countThreads = 3; static List<Thread> threads = new ArrayList<Thread>(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++) { 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(); } } } class Water implements Runnable { private String commonResource; public Water(String commonResource) { this.commonResource = commonResource; } public void run() { boolean isCurrentThreadInterrupted = Thread.currentThread().interrupted(); String threadName = Thread.currentThread().getName(); try { while (!isCurrentThreadInterrupted) { System.out.println("Объект " + commonResource + ", нить " + threadName + " interrupted? " + isCurrentThreadInterrupted); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("нить " + threadName + " исключение " + e); } } } 

The thread stops due to an exception.

  • Thank you very much just now I noticed traeche and forgot that I’m interuptide in a slip and expelling the user - user7351520