2

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.

Bentaye
  • 9,403
  • 5
  • 32
  • 45
Tristate
  • 1,498
  • 2
  • 18
  • 38
  • 1
    Is the access directly to the attribute, or is it private and it is accessed by a getter? – Bentaye Jun 11 '19 at 08:03
  • What kind of review have you tried so far? You should be able to track all classes that implements `Runnable` or inherit from `Thread`, if any of this access the variable, it could be potentially shared. – bracco23 Jun 11 '19 at 08:04
  • Also, this might be helpful: https://dzone.com/articles/checkthread-a-static-analysis-tool-for-java-concurrency-bugs – bracco23 Jun 11 '19 at 08:05
  • @bracco23 There are some PropertyChangeListener that accessing it. No one of the classes implements Runnable or extends Thread class. – Tristate Jun 11 '19 at 08:16
  • @Bentaye its private and is accessed by a setter – Tristate Jun 11 '19 at 08:17

2 Answers2

3

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:

class MyClass {

    private boolean myAttribute = false;

    private Set<String> threads = new HashSet<>();
    public Set<String> getThreadsSet() {
        return threads;
    }

    public boolean isMyAttribute() {
        synchronized (threads) {
            threads.add(Thread.currentThread().getName());
        }
        return myAttribute;
    }

}

Then you can test

MyClass c = new MyClass();

Runnable runnable = c::isMyAttribute;

Thread thread1 = new Thread(runnable, "t1");
Thread thread2 = new Thread(runnable, "t2");
Thread thread3 = new Thread(runnable, "t3");

thread1.start();
thread2.start();
thread3.start();

thread1.join();
thread2.join();
thread3.join();

System.out.println(c.getThreadsSet());

This outputs:

[t1, t2, t3]

EDIT: Just saw that you added that the attribute is accessed via setter, you can adapt the solution and log Threads in the setter

Bentaye
  • 9,403
  • 5
  • 32
  • 45
0

Always access the variable with a getter and write down a logic to obtain the thread id whenever a new thread tries to get the primitive value . whenever the thread terminates , use shutdown hook to remove that threadId from that list. The list will contain the Ids of all the threads currently holding the reference to that variable.

 getVar(){
countLogic();
return var;}

countLogic(){
if(!list.contains(Thread.getCurrentThread().getId)){
list.add(Thread.getCurrentThread().getId);
Runtime.getRuntime().addShutdownHook(//logic to remove thread id from the list);
}

Hope it helps

jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41