How to find how many times a method is used?

A general answer to this question would be something like this:

Static variable

1
2
3
4
5
6
7
8
public class MyClass {
static int count = 0;
public void myMethod() {
count++;
}
}

Using static int would seem a perfect solution. Static will share the instance variable count among all instances of MyClass.
However, problem occurs in a multi threaded environment, as the count incrementation is not synchronised.

Multi-threaded

1
2
3
4
5
6
7
8
9
10
11
12
package com.deepumohan.tech.methodinvocationcount;
public class MyClass {
static int count = 0;
public void myMethod() {
synchronized(this) {
count++;
}
}
}

Now this works perfect in a multi-threaded environment.

Thread safe atomic class

Java provides a better option to use thread-safe increments through Atomic classes available in java.util.concurrent.atomic packag.

1
2
3
4
5
6
7
8
9
10
package com.deepumohan.tech.methodinvocationcount;
import java.util.concurrent.atomic.AtomicInteger;
public class MyClass {
AtomicInteger atomicInteger = new AtomicInteger(0);
public int getMethodInvocationCount() {
return atomicInteger.incrementAndGet();
}
}

Here is a good read about AtomicInteger vs static int: Java - using AtomicInteger vs Static int