-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
122 lines (93 loc) · 3.5 KB
/
Solution.java
File metadata and controls
122 lines (93 loc) · 3.5 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
//If I had not come and spoken to them, they would not have had sin; but now they have no excuse for their sin. (John 15:22)
package com.javarush.task.task16.task1606;
import java.util.ArrayList;
import java.util.List;
/*
join: в нужное время в нужном месте
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
PrintListThread firstThread = new PrintListThread("firstThread");
PrintListThread secondThread = new PrintListThread("secondThread");
firstThread.start();
firstThread.join();
secondThread.start();
}
public static void printList(List<String> list, String threadName) {
for (String item : list) {
System.out.println(String.format("%s : %s", threadName, item));
}
}
public static List<String> getList(int n) {
List<String> result = new ArrayList<String>();
if (n < 1) return result;
for (int i = 0; i < n; i++) {
result.add(String.format("String %d", (i + 1)));
}
return result;
}
public static class PrintListThread extends Thread {
public PrintListThread(String name) {
super(name);
}
public void run() {
printList(getList(20), getName());
}
}
}
/*
join: в нужное время в нужном месте
Подумай, в каком месте и для какого объекта нужно вызвать метод join, чтобы результат выводился по-порядку
сначала для firstThread, а потом для secondThread.
Вызови метод join в нужном месте.
Пример вывода:
firstThread : String 1
firstThread : String 2
...
firstThread : String 19
firstThread : String 20
secondThread : String 1
...
secondThread : String 20
Требования:
1. Метод main должен вызывать метод join для объекта firstThread.
2. Метод main не должен вызывать метод join для объекта secondThread.
3. Метод main не должен вызывать System.out.println() или System.out.print().
4. Вывод программы должен соответствовать примеру из задания.
package com.javarush.task.task16.task1606;
import java.util.ArrayList;
import java.util.List;
/*
join: в нужное время в нужном месте
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
PrintListThread firstThread = new PrintListThread("firstThread");
PrintListThread secondThread = new PrintListThread("secondThread");
firstThread.start();
//firstThread.join();
secondThread.start();
}
public static void printList(List<String> list, String threadName) {
for (String item : list) {
System.out.println(String.format("%s : %s", threadName, item));
}
}
public static List<String> getList(int n) {
List<String> result = new ArrayList<String>();
if (n < 1) return result;
for (int i = 0; i < n; i++) {
result.add(String.format("String %d", (i + 1)));
}
return result;
}
public static class PrintListThread extends Thread {
public PrintListThread(String name) {
super(name);
}
public void run() {
printList(getList(20), getName());
}
}
}
*/