//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 notes = new ArrayList();
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 notes = new ArrayList();
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 + "]");
}
}
}
}
*/