Context:
I have created a small (java) multithread server for a game. Despite my best efforts of following the best practices it turned out that some methods that were intended to be called from just one thread got called from 2 or more threads. After debugging and analysis I have managed to "fix" my design but I was wondering:
The question:
Is there a tool (or if not - is it possible (and how) to be developed) that allows you to mark some methods with annotations like @SingleThread
or @ThreadCount(2)
or @ThreadNameLike("my_fancy_thread_group*")
which counts/monitors/logs access to these methods like:
@SingleThread
- checks if this method is always accessed by only thread@ThreadCount(2)
- is accessed by two threads exactly@ThreadNameLike
- is accessed only by threads with name matching the pattern(s)
The idea is to do a TEST run of the program and get at least log record that annotated condition is violated.
I was thinking that probably AspectJ can do the job to some extend with it's pointcuts but then I realized that some approach similar to Dagger / Dagger2 will be better, i.e. when you want to test your server you will have to turn on an annotation processor (let's called it hypothetically "SafetyFirst") which will generate adapter (wrapper?) classes which contain the monitoring code. Then you will run the server, run some load tests and then check the logs for violations (or in an ideal world - get a report file).
I fully realize that such tool:
- will not provide 100% coverage of all potential cases;
- will mask/trigger heisenbugs
- will slow down the examined program
but at very least it can serve as an early warning system that will clearly advertise violations of the intended design.