I have a variable in my code, a simple primitive boolean x. Because of code complexity, I am not sure about the number of threads accessing it. Maybe it is never shared, or is used by only one thread, maybe not. If it is shared between threads, I need to use AtomicBoolean instead.
Is there a way to count threads accessing boolean x?
Until now I did a review of the code but it is very complex and not written by me.
If this is just for testing/debugging purpose, you could maybe do it this way:
If not yet the case, expose the boolean via a getter and count the threads in the getter. Here is a simple example where I make a list of all the threads accessing the getter:
Then you can test
This outputs:
EDIT: Just saw that you added that the attribute is accessed via setter, you can adapt the solution and log Threads in the setter