views:

64

answers:

2

Hi,

I'm using net beans for Java development. I'm working on a multi threading application and I want to easily identify code sections which are executed by more than one thread? Is there a easy way to do that?

For example, if some field of method of class ABC is executed by more than one thread?

+2  A: 

In general, it is not possible to do statically, i.e. by inspection of the code. (The problem is undecidable due to the halting problem.)

Your only option is to do the analysis runtime, that is, to log which actual thread executes with method. You have a few options. Here are two that I come to think of immediately.

  • Add System.out.println(Thread.currentThread()) on interesting methods
  • Use for example AspectJ to do something similar.
aioobe
+2  A: 

This is something that can only be determined at runtime.

You can throw this method to the beginning you your method calls to determine the calling Thread.

public static void reportThread(String methodName) {
    //Somehow LOG (println, logging framework)
    LOG(methodName + " was ran on thread: " + Thread.currentThread().getName());

}
jjnguy