-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
139 lines (105 loc) · 4.89 KB
/
Solution.java
File metadata and controls
139 lines (105 loc) · 4.89 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
// Again, "Praise the Lord, all you Gentiles! Let all the peoples praise him." (Romans 15:11)
package com.javarush.task.task16.task1628;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Solution {
public static volatile AtomicInteger countReadStrings = new AtomicInteger(0);
public static volatile BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
//read count of strings
int count = Integer.parseInt(reader.readLine());
//init threads
ReaderThread consolReader1 = new ReaderThread();
ReaderThread consolReader2 = new ReaderThread();
ReaderThread consolReader3 = new ReaderThread();
consolReader1.start();
consolReader2.start();
consolReader3.start();
while (count > countReadStrings.get()) {
}
consolReader1.interrupt();
consolReader2.interrupt();
consolReader3.interrupt();
System.out.println("#1:" + consolReader1);
System.out.println("#2:" + consolReader2);
System.out.println("#3:" + consolReader3);
reader.close();
}
public static class ReaderThread extends Thread {
private List<String> result = new ArrayList<String>();
public void run() {
while (!isInterrupted()) { //add your code here - добавьте код тут
try {
if (reader.ready()) {
result.add(reader.readLine());
countReadStrings.incrementAndGet();
}
} catch (IOException e) {e.printStackTrace(); }
}
}
@Override
public String toString() {
return result.toString();
}
}
}
/*
Кто первый встал - того и тапки
1. Разберись, что делает программа.
1.1. Каждая нить должна читать с консоли слова. Используй готовый static BufferedReader reader.
1.2. Используй AtomicInteger countReadStrings, чтобы посчитать, сколько слов уже считано с консоли всеми нитями.
2. Реализуй логику метода run:
2.1. Пока нить не прервана (!isInterrupted) читай с консоли слова и добавляй их в поле List<String> result.
2.2. Используй countReadStrings для подсчета уже считанных с консоли слов.
Требования:
1. Метод run должен работать пока нить не прервана (!isInterrupted).
2. Метод run НЕ должен создавать свои InputStreamReader-ы или BufferedReader-ы.
3. Метод run должен считывать слова из reader и добавлять их в список result.
4. Метод run должен после каждого считывания увеличивать счетчик прочитанных строк countReadStrings на 1.
5. Программа должна выводить данные, считанные каждым потоком.
package com.javarush.task.task16.task1628;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Solution {
public static volatile AtomicInteger countReadStrings = new AtomicInteger(0);
public static volatile BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
//read count of strings
int count = Integer.parseInt(reader.readLine());
//init threads
ReaderThread consolReader1 = new ReaderThread();
ReaderThread consolReader2 = new ReaderThread();
ReaderThread consolReader3 = new ReaderThread();
consolReader1.start();
consolReader2.start();
consolReader3.start();
while (count > countReadStrings.get()) {
}
consolReader1.interrupt();
consolReader2.interrupt();
consolReader3.interrupt();
System.out.println("#1:" + consolReader1);
System.out.println("#2:" + consolReader2);
System.out.println("#3:" + consolReader3);
reader.close();
}
public static class ReaderThread extends Thread {
private List<String> result = new ArrayList<String>();
public void run() {
//add your code here - добавьте код тут
}
@Override
public String toString() {
return result.toString();
}
}
}
*/