package ru.agolovin;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author agolovin (agolovin@list.ru)
 * @version $Id$
 * @since 0.1
 */

@ThreadSafe
class Count {

    /**
     * Expected result.
     */
    private static final int LIMIT = 100_000_000;

    /**
     * Count.
     */
    private static int anInt = 0;

    /**
     * Count hits.
     */
    private AtomicInteger lim = new AtomicInteger();

    /**
     * Main method.
     *
     * @param args String[]
     */
    public static void main(String[] args) {
        Count count = new Count();
        Thread thread = new Thread(() -> {
            while (!count.stop()) {
                count.increment();
            }
        });
        Thread thread2 = new Thread(() -> {
            while (!count.stop()) {
                count.increment();
            }

        });
        thread.start();
        thread2.start();
        try {
            thread.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(String.format("Expected %s", LIMIT));
        System.out.println(String.format("Result %s", anInt));
    }

    /**
     * Stop.
     *
     * @return boolean result
     */
    private boolean stop() {
        return lim.incrementAndGet() > LIMIT;
    }

    @GuardedBy("this")
    private void increment() {
        synchronized (this) {
            anInt++;
        }
    }
}

@@ -377,30 +377,46 @@
  */%0A    
+@GuardedBy(%22This%22)%0A    
 private
- static
  int anI
@@ -1269,16 +1269,22 @@
 lt %25s%22, 
+count.
 anInt));
@@ -1447,26 +1447,43 @@
     
-@GuardedBy(%22this%22)
+/**%0A     * Increment value.%0A     */
 %0A   
@@ -1552,16 +1552,21 @@
         
+this.
 anInt++;

add task1 in part3 chapter7 package2