-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
142 lines (109 loc) · 4.66 KB
/
Solution.java
File metadata and controls
142 lines (109 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//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)
package com.javarush.task.task16.task1620;
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++) { //add your code here - добавь код тут
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();
}
}
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().isInterrupted();
String threadName = Thread.currentThread().getName();
try {
while (!isCurrentThreadInterrupted) {
System.out.println("Объект " + commonResource + ", нить " + threadName);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
}
}
/*
Один для всех, все - для одного
1. Разберись, как работает программа.
1.1. Обрати внимание, что объект Water — один для всех нитей.
2. Реализуй метод ourInterruptMethod, чтобы он прерывал все нити из threads.
3. В методе run исправь значения переменных:
3.1. isCurrentThreadInterrupted — должна равняться значению метода isInterrupted у текущей нити.
3.2. threadName — должна равняться значению метода getName (реализовано в классе Thread) у текущей нити.
Требования:
1. Метод ourInterruptMethod должен прервать все нити из списка threads.
2. Метод run должен получать текущую нить с помощью Thread.currentThread.
3. Метод run должен использовать метод isInterrupted текущей нити.
4. Метод run должен использовать метод getName текущей нити.
5. Метод main должен работать примерно 3 сек.
package com.javarush.task.task16.task1620;
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() {
//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 = false;
String threadName = "";
try {
while (!isCurrentThreadInterrupted) {
System.out.println("Объект " + commonResource + ", нить " + threadName);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
}
}
*/