问题
The Java Language Specification 7 (JLS7-17.4.5) defines a "correctly synchronized" program like this: "A program is correctly synchronized if and only if all sequentially consistent executions are free of data races".
JLS7-17.4.5 also states that:
Without correct synchronization, very strange, confusing and counterintuitive behaviors are possible.
So, from a programmer's point of view, it would be very useful to have a tool to determine whether a program is "correctly synchronized" according the the above definition.
Is there such a tool? I could not find anything by Googling on it. If there is no such tool, would it be possible to make one?
回答1:
FindBugs can find some concurrency bugs (search for "Multithreaded correctness" on the list of detected bugs) and there probably are other similar tools, but in the end some bugs can only be avoided with meticulous code design and review.
You can also test your classes for concurrency issues, but it is a game of statistics and some bugs might never show up depending on OS/CPU architecture etc.
I have heard about the Java concurrency torture tool, but never used it. It has been mentioned several times on the official JSR 166 Concurrency Interest list.
来源:https://stackoverflow.com/questions/14400044/is-there-a-tool-to-determine-whether-a-program-is-correctly-synchronized-as-de