How to identify sections of code executed by multiple threads
I'm using net beans for Java development. I'm working on a multi threading appl开发者_运维知识库ication 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?
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());
}
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.
精彩评论