ΡΡΠΎ Π½Π΅ ΡΠ°ΠΊ?
package com.javarush.task.task17.task1705;
import java.util.ArrayList;
import java.util.List;
/*
Π‘Π°Π΄-ΠΎΠ³ΠΎΡΠΎΠ΄
*/
public class Solution {
public static void main(String[] args) {
}
public static class Garden {
public final List<String> fruits = new ArrayList<String>();
public final List<String> vegetables = new ArrayList<String>();
Object lock1 = new Object();
Object lock2 = new Object();
Object lock3 = new Object();
Object lock4 = new Object();
public void addFruit (int index, String fruit){
synchronized (lock1) {
for (int i = 0; i < fruits.size(); i++) {
fruits.add(index, fruit);
}
}
}
public void removeFriuit(int index){
synchronized (lock2){fruits.remove(index);}
}
public void addVegetable(int index, String vegetable){
synchronized (lock3) {
for (int i = 0; i < vegetables.size(); i++) {
vegetables.add(index, vegetable);
}
}
}
public void removeVegetable(int index){
synchronized (lock4){vegetables.remove(index);}
}
}
}