-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
101 lines (79 loc) · 3.45 KB
/
Solution.java
File metadata and controls
101 lines (79 loc) · 3.45 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
//Until now, you have asked nothing in my name. Ask, and you will receive, that your joy may be made full. (John 16:24)
package com.javarush.task.task17.task1701;
import java.util.ArrayList;
import java.util.List;
/*
Заметки
*/
public class Solution {
public static void main(String[] args) {
new NoteThread().start();
new NoteThread().start();
}
public static class Note {
public static final List<String> notes = new ArrayList<String>();
public static void addNote(String note) {
notes.add(0, note);
}
public static void removeNote(String threadName) {
String note = notes.remove(0);
if (note == null) {
System.out.println("Другая нить удалила нашу заметку");
} else if (!note.startsWith(threadName)) {
System.out.println("Нить [" + threadName + "] удалила чужую заметку [" + note + "]");
}
}
}
public static class NoteThread extends Thread {
public void run() {
int index = 0;
while(index < 1000) {
Note.addNote(getName() + "-Note" + index);
Note.removeNote(getName());
index++;
}
}
}
}
/*
Заметки
Асинхронность выполнения нитей.
1. Класс Note будет использоваться нитями.
2. Создай public static нить NoteThread (Runnable не является нитью), которая в методе run 1000 раз (index = 0-999) сделает следующие действия:
2.1. Используя метод addNote добавит заметку с именем [getName() + «-Note» + index], например, при index=4
«Thread-0-Note4»
2.2. Используя метод removeNote удалит заметку
2.3. В качестве параметра в removeNote передай имя нити — метод getName()
Требования:
1. Класс Solution должен содержать public static класс NoteThread.
2. Класс NoteThread должен быть нитью.
3. В методе run класса NoteThread должен быть цикл.
4. Метод run класса NoteThread должен 1000 раз вызывать метод addNote c параметром (getName() + "-Note" + index).
5. Метод run класса NoteThread должен 1000 раз вызывать метод removeNote c параметром (getName()).
package com.javarush.task.task17.task1701;
import java.util.ArrayList;
import java.util.List;
*
Заметки
*
public class Solution {
public static void main(String[] args) {
new NoteThread().start();
new NoteThread().start();
}
public static class Note {
public static final List<String> notes = new ArrayList<String>();
public static void addNote(String note) {
notes.add(0, note);
}
public static void removeNote(String threadName) {
String note = notes.remove(0);
if (note == null) {
System.out.println("Другая нить удалила нашу заметку");
} else if (!note.startsWith(threadName)) {
System.out.println("Нить [" + threadName + "] удалила чужую заметку [" + note + "]");
}
}
}
}
*/