//In that day you will ask in my name; and I don't say to you, that I will pray to the Father for you (John 16:26)
package com.javarush.task.task17.task1703;
import java.util.ArrayList;
import java.util.List;
/*
СинÑ
ÑонизиÑованнÑе замеÑки
*/
public class Solution {
public static void main(String[] args) {
}
public static class Note {
public final List notes = new ArrayList();
public void addNote(int index, String note) {
System.out.println("СейÑÐ°Ñ Ð±ÑÐ´ÐµÑ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð° замеÑка [" + note + "] Ðа позиÑÐ¸Ñ " + index);
synchronized(notes) {
notes.add(index, note);
}
System.out.println("Уже добавлена замеÑка [" + note + "]");
}
public void removeNote(int index) {
System.out.println("СейÑÐ°Ñ Ð±ÑÐ´ÐµÑ Ñдалена замеÑка Ñ Ð¿Ð¾Ð·Ð¸Ñии " + index);
String note;
synchronized(notes) {
note = notes.remove(index);
}
System.out.println("Уже Ñдалена замеÑка [" + note + "] Ñ Ð¿Ð¾Ð·Ð¸Ñии " + index);
}
}
}
/*
СинÑ
ÑонизиÑованнÑе замеÑки
1. ÐлаÑÑ Note бÑÐ´ÐµÑ Ð¸ÑполÑзоваÑÑÑÑ Ð½Ð¸ÑÑми. ÐоÑÑÐ¾Ð¼Ñ Ñделай Ñак, ÑÑÐ¾Ð±Ñ Ð¾Ð±ÑаÑÐµÐ½Ð¸Ñ Ðº лиÑÑÑ notes блокиÑовали мÑÑÑÐµÐºÑ notes, не this
2. ÐÑе System.out.println не Ð´Ð¾Ð»Ð¶Ð½Ñ Ð±ÑÑÑ Ð·Ð°Ð±Ð»Ð¾ÐºÐ¸ÑÐ¾Ð²Ð°Ð½Ñ (ÑинÑ
ÑонизиÑованÑ), Ñ.е. не Ð´Ð¾Ð»Ð¶Ð½Ñ Ð½Ð°Ñ
одиÑÑÑÑ Ð² блоке synchronized
ТÑебованиÑ:
1.âÐеÑод addNote() должен добавлÑÑÑ Ð·Ð°Ð¿Ð¸Ñки в ÑпиÑок notes.
2.âÐеÑод removeNote() должен ÑдалÑÑÑ Ð·Ð°Ð¿Ð¸ÑÐºÑ Ð¸Ð· ÑпиÑка notes.
3.âРмеÑоде addNote() должен наÑ
одиÑÑÑÑ synchronized блок.
4.âРмеÑоде removeNote() должен наÑ
одиÑÑÑÑ synchronized блок.
5.âSynchronized блок в меÑоде addNote() должен блокиÑоваÑÑ Ð¼ÑÑÑÐµÐºÑ notes.
6.âSynchronized блок в меÑоде removeNote() должен блокиÑоваÑÑ Ð¼ÑÑÑÐµÐºÑ notes.
7.âÐ synchronized блоке меÑода addNote() должен наÑ
одиÑÑÑ Ð²Ñзов меÑода add Ñ notes.
8.âÐ synchronized блоке меÑода removeNote() должен наÑ
одиÑÑÑ Ð²Ñзов меÑода remove Ñ notes.
9.âÐÑе ÐºÐ¾Ð¼Ð°Ð½Ð´Ñ Ð²Ñвода на ÑкÑан не Ð´Ð¾Ð»Ð¶Ð½Ñ Ð½Ð°Ñ
одиÑÑÑÑ Ð² блоке synchronized.
package com.javarush.task.task17.task1703;
import java.util.ArrayList;
import java.util.List;
*
СинÑ
ÑонизиÑованнÑе замеÑки
*
public class Solution {
public static void main(String[] args) {
}
public static class Note {
public final List notes = new ArrayList();
public void addNote(int index, String note) {
System.out.println("СейÑÐ°Ñ Ð±ÑÐ´ÐµÑ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð° замеÑка [" + note + "] Ðа позиÑÐ¸Ñ " + index);
notes.add(index, note);
System.out.println("Уже добавлена замеÑка [" + note + "]");
}
public void removeNote(int index) {
System.out.println("СейÑÐ°Ñ Ð±ÑÐ´ÐµÑ Ñдалена замеÑка Ñ Ð¿Ð¾Ð·Ð¸Ñии " + index);
String note = notes.remove(index);
System.out.println("Уже Ñдалена замеÑка [" + note + "] Ñ Ð¿Ð¾Ð·Ð¸Ñии " + index);
}
}
}
*/