Count of request per second, minute in java I want to count number of requests per second, minute. I want do it fast and thread-safe. I have 2 variables to count:
private AtomicInteger count;
private AtomicInteger minutesCount;

And 2 variables for last second and minute:
private AtomicInteger currentSecond;
private AtomicInteger currentMinute;

Constructor of my service:
public StatisticServiceImpl() {
   final Calendar calendar = Calendar.getInstance();

   this.currentSecond = new AtomicInteger(calendar.get(Calendar.SECOND));
   this.currentMinute = new AtomicInteger(calendar.get(Calendar.MINUTE));

   this.count = new AtomicInteger();
   this.minutesCount = new AtomicInteger();
}

And my method to count:
public void count() {
   final Calendar calendar = Calendar.getInstance();
   int newSecond = calendar.get(Calendar.SECOND);
   int newMinute = calendar.get(Calendar.MINUTE);

   currentSecond.getAndUpdate(prev -&gt; {
      //if it is new second, then clear second count and set new current second
      if (newSecond != prev) {
         count = new AtomicInteger();
         return newSecond;
      } else {
         count.incrementAndGet();
         return prev;
      }
   });

   currentMinute.getAndUpdate(prev -&gt; {
      //if it is new minute, then clear minute count and set new current minute
      if (newMinute != prev) {
         minutesCount = new AtomicInteger();
         return newMinute;
      } else {
         minutesCount.incrementAndGet();
         return prev;
      }
   });
}

So, is it better (in a sense of performance and accuracy) then solution from this post?
(use LinkedList)
Or suggest better solution for this, please.
The first thing I noticed.
The calendar in the constructor. If you wanted to test this class it would be really difficult because you
are dependent on the Calendar implementation, and you have no control over the value it will return. It would be better to inject the calendar in to the
constructor, or Abstract the desired functionality in to an interface and inject that. This will also allow you to use one instance for your entire application.
instead of constructing a separate one in the count method.
injecting the calendar:
public StatisticService(final Calendar calendar) {
        this.calender = calendar;
        this.currentSecond = new AtomicInteger(calendar.get(Calendar.SECOND));
        this.currentMinute = new AtomicInteger(calendar.get(Calendar.MINUTE));
        this.count = new AtomicInteger();
        this.minutesCount = new AtomicInteger();
    }

Creating an interface:
Using an interface gives you flexibility around the implementation of the counter. If in the future you want to change from using
a Calendar to something else only the code implementing the interface will change and classes dependent on it will be unchanged.
public interface TimeProviderService {
    Integer getSecond();

    Integer getMinute();
}

Second
It is a lot more expensive to create a new AtomicInteger class then to just set it to zero.
So here instead of constructing a new class just reset the integer.
//if it is new second, then clear second count and set new current second
            if (newSecond != prev) {
                count.set(0);
                return newSecond;
            } else {
                count.incrementAndGet();
                return prev;
            }

Third suggestions for locking.
Atomic classes are great but in this instance since you are wanting the entire count method to
be atomic I would suggest using your own lock. It simplifies the code for people trying to read it later and the entire
method would be synchronized instead of just when you're accessing the atomic integers.
Also, locking inside of a lock is asking for a deadlock. See Here.
currentMinute.getAndUpdate(prev -&gt; {
            ...
                minutesCount.incrementAndGet();
            ...
        });


Finally
Here is what the class would look like if you implemented the suggestions I am proposing.
Time Providers
public interface TimeProviderService {
    Integer getSecond();

    Integer getMinute();
}

public class CalendarTimeProviderService implements TimeProviderService {
    private final Calendar calendar;

    public CalendarTimeProviderService(Calendar calendar) {
        this.calendar = calendar;
    }

    @Override
    public Integer getSecond() {
        return this.calendar.get(Calendar.SECOND);
    }

    @Override
    public Integer getMinute() {
        return this.calendar.get(Calendar.MINUTE);
    }
}

Statistic Service
public class StatisticService {
    private final TimeProviderService timeProviderService;
    private final Lock lock = new ReentrantLock();
    private int currentSecond;
    private int currentMinute;
    private int count;
    private int minutesCount;


    public StatisticService(TimeProviderService timeProviderService) {
        this.timeProviderService = timeProviderService;

        this.currentSecond = this.timeProviderService.getSecond();
        this.currentMinute = this.timeProviderService.getMinute();

        this.count = 0;
        this.minutesCount = 0;
    }


    public void count() {
        this.lock.lock();

        try {
            int newSecond = this.timeProviderService.getSecond();
            int newMinute = this.timeProviderService.getMinute();


            // seconds
            if (newSecond != currentSecond) {
                this.count = 0;
                this.currentSecond = newSecond;
            } else {
                this.count = this.count + 1;
            }

            // minutes
            if (newMinute != currentMinute) {
                this.minutesCount = 0;
                this.currentMinute = newMinute;
            } else {
                this.minutesCount = this.minutesCount + 1;
            }


        } finally {
            // make sure we are unlocking no matter what happens
            this.lock.unlock();
        }
    }
}

one final note
I did not change anything about how you were calculating the time I just changed the way you are doing it. I do not the problem you
are trying to solve, so I cannot speak on the best way.