package ru.job4j.cacheStoringModels;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@ThreadSafe
public class CacheStoringModels extends Thread {

    @GuardedBy("this")
    public ConcurrentHashMap<Integer, Base> map = new ConcurrentHashMap<>();

    private EmulatedCAS value;

    public void add(Base model) {
        map.put(model.getId(), model);
    }

    public void update(Base model) throws OptimisticException {
        value = new EmulatedCAS(model.getVersion());
        int oldValue = value.getValue();
        if (value.compareAndSwap(oldValue, oldValue + 1) != oldValue) {
            oldValue = model.getVersion();
            throw new OptimisticException("Данные уже обновлены");
        }
        model.setVersion(oldValue + 1);
    }


    public static void main(String[] args) throws InterruptedException {
        CacheStoringModels csm = new CacheStoringModels();
        Base model = new Base(0,0);
        csm.add(model);

            Thread task_1 = new Thread() {
                @Override
                public void run() {
                    try {
                        csm.update(model);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            Thread task_2 = new Thread() {
                @Override
                public void run() {
                    try {
                        csm.update(model);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            };

            task_1.start();
            task_2.start();

    }

}

@@ -269,23 +269,8 @@
 els 
-extends Thread 
 %7B%0A%0A 
@@ -381,27 +381,19 @@
 private 
-EmulatedCAS
+int
  value;%0A
@@ -548,33 +548,21 @@
     
- 
+if (
 value =
+=
  
-new EmulatedCAS(
 mode
@@ -576,17 +576,18 @@
 rsion())
-;
+ %7B
 %0A       
@@ -591,155 +591,156 @@
     
-int oldValue = value.getValue();%0A        if (value.compareAndSwap(oldValue, oldValue + 1) != oldValue) %7B%0A            oldValue = model.getVersion();
+   value = model.getVersion();%0A           model.setVersion(value+1);%0A           map.computeIfPresent(model.getId(), (k, v)-%3E model);%0A       %7D else %7B
 %0A   
@@ -783,14 +783,14 @@
 on(%22
-%D0%94%D0%B0%D0%BD%D0%BD%D1%8B%D0%B5
+%D0%9C%D0%BE%D0%B4%D0%B5%D0%BB%D1%8C
  %D1%83%D0%B6%D0%B5
@@ -802,9 +802,9 @@
 %D0%B2%D0%BB%D0%B5%D0%BD
-%D1%8B
+%D0%B0
 %22);%0A
@@ -814,48 +814,99 @@
     
- 
 %7D%0A    
+%7D%0A%0A
     
-model.setVersion(oldValue + 1
+public synchronized void delete(Base model) %7B%0A        map.remove(model.getId()
 );%0A 
@@ -1735,37 +1735,312 @@
 %0A            %7D;%0A
-%0A
     
+    Thread task_3 = new Thread() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    csm.update(model);%0A                %7D catch (Exception e) %7B%0A                    e.printStackTrace();%0A                %7D%0A%0A            %7D%0A        %7D;%0A%0A
         task_1.s
@@ -2043,28 +2043,24 @@
 _1.start();%0A
-    
         task
@@ -2067,22 +2067,21 @@
 _2.start();%0A
-%0A
     %7D%0A%0A%7D%0A

task #187611