package com.codegym.task.task16.task1620;
import java.util.ArrayList;
import java.util.List;
/*
One for all, all for one
*/
public class Solution {
public static byte countThreads = 3;
static List<Thread> threads = new ArrayList<>(countThreads);
public static void main(String[] args) throws InterruptedException {
initThreadsAndStart();
Thread.sleep(3000);
ourInterrupt();
}
public static void ourInterrupt() {
//write your code here
for (int i = 0; i < threads.size(); i++) {
threads.get(i).interrupt();
}
}
private static void initThreadsAndStart() {
Water water = new Water("water");
for (int i = 0; i < threads.size(); i++) {
threads.add(new Thread(water, "#" + i));
}
for (int i = 0; i < threads.size(); 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
boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted();
String threadName = Thread.currentThread().getName();
try {
while (!isCurrentThreadInterrupted) {
System.out.println("Object " + sharedResource + ", thread " + threadName);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
}
}
am i missing something? first requirement wont pass.
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
4 July 2020, 13:28
strange, this should work. The below code validated for me. Maybe you want to try it and see if it's your loop or something else.
0
Tiko hakobyan
6 July 2020, 12:32
thanks for the replay
i've tried enhanced for loop which did not pass the validation as well
0
Thomas
6 July 2020, 13:03solution
I just see, that you modified the given code. Maybe that's a problem for validation. I'd reset the task and retry. Other than that I can't spot any problem.
+2
Tiko hakobyan
6 July 2020, 13:18
thank you very much, looks like i have deleted that part of the code for some reason i cant recall at the moment and tried to wright it down from memory after that.
0