Skip to content

Commit 66a33fc

Browse files
authored
your sorrow shall be turned into joy
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)
1 parent 7368934 commit 66a33fc

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

task16/task1620/Solution.java

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
//Verily, verily, I say unto you, That ye shall weep and lament, but the world shall rejoice:
3+
//and ye shall be sorrowful, but your sorrow shall be turned into joy. (John 16:20)
4+
5+
package com.javarush.task.task16.task1620;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/*
11+
Один для всех, все - для одного
12+
*/
13+
14+
public class Solution {
15+
public static byte countThreads = 3;
16+
static List<Thread> threads = new ArrayList<Thread>(countThreads);
17+
18+
public static void main(String[] args) throws InterruptedException {
19+
initThreadsAndStart();
20+
Thread.sleep(3000);
21+
ourInterruptMethod();
22+
}
23+
24+
public static void ourInterruptMethod() {
25+
for (int i = 0; i < countThreads; i++) { //add your code here - добавь код тут
26+
threads.get(i).interrupt();
27+
}
28+
}
29+
30+
private static void initThreadsAndStart() {
31+
Water water = new Water("water");
32+
for (int i = 0; i < countThreads; i++) {
33+
threads.add(new Thread(water, "#" + i));
34+
}
35+
36+
for (int i = 0; i < countThreads; i++) {
37+
threads.get(i).start();
38+
}
39+
}
40+
41+
public static class Water implements Runnable {
42+
private String commonResource;
43+
44+
public Water(String commonResource) {
45+
this.commonResource = commonResource;
46+
}
47+
48+
public void run() {
49+
//fix 2 variables - исправь 2 переменных
50+
boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted();
51+
String threadName = Thread.currentThread().getName();
52+
53+
try {
54+
while (!isCurrentThreadInterrupted) {
55+
56+
System.out.println("Объект " + commonResource + ", нить " + threadName);
57+
Thread.sleep(1000);
58+
}
59+
} catch (InterruptedException e) {
60+
}
61+
}
62+
}
63+
}
64+
65+
/*
66+
Один для всех, все - для одного
67+
1. Разберись, как работает программа.
68+
1.1. Обрати внимание, что объект Water — один для всех нитей.
69+
2. Реализуй метод ourInterruptMethod, чтобы он прерывал все нити из threads.
70+
3. В методе run исправь значения переменных:
71+
3.1. isCurrentThreadInterrupted — должна равняться значению метода isInterrupted у текущей нити.
72+
3.2. threadName — должна равняться значению метода getName (реализовано в классе Thread) у текущей нити.
73+
74+
75+
Требования:
76+
1. Метод ourInterruptMethod должен прервать все нити из списка threads.
77+
2. Метод run должен получать текущую нить с помощью Thread.currentThread.
78+
3. Метод run должен использовать метод isInterrupted текущей нити.
79+
4. Метод run должен использовать метод getName текущей нити.
80+
5. Метод main должен работать примерно 3 сек.
81+
82+
83+
package com.javarush.task.task16.task1620;
84+
85+
import java.util.ArrayList;
86+
import java.util.List;
87+
88+
*
89+
Один для всех, все - для одного
90+
*
91+
92+
public class Solution {
93+
public static byte countThreads = 3;
94+
static List<Thread> threads = new ArrayList<Thread>(countThreads);
95+
96+
public static void main(String[] args) throws InterruptedException {
97+
initThreadsAndStart();
98+
Thread.sleep(3000);
99+
ourInterruptMethod();
100+
}
101+
102+
public static void ourInterruptMethod() {
103+
//add your code here - добавь код тут
104+
}
105+
106+
private static void initThreadsAndStart() {
107+
Water water = new Water("water");
108+
for (int i = 0; i < countThreads; i++) {
109+
threads.add(new Thread(water, "#" + i));
110+
}
111+
112+
for (int i = 0; i < countThreads; i++) {
113+
threads.get(i).start();
114+
}
115+
}
116+
117+
public static class Water implements Runnable {
118+
private String commonResource;
119+
120+
public Water(String commonResource) {
121+
this.commonResource = commonResource;
122+
}
123+
124+
public void run() {
125+
//fix 2 variables - исправь 2 переменных
126+
boolean isCurrentThreadInterrupted = false;
127+
String threadName = "";
128+
129+
try {
130+
while (!isCurrentThreadInterrupted) {
131+
132+
System.out.println("Объект " + commonResource + ", нить " + threadName);
133+
Thread.sleep(1000);
134+
}
135+
} catch (InterruptedException e) {
136+
}
137+
}
138+
}
139+
}
140+
141+
142+
*/

0 commit comments

Comments
 (0)