multithreading - What is a good way to test that a Java method is synchronized? -
i have several classes implement interface. interface has contract, methods should synchronized, , should not, , want verify contract through unit tests implementations. methods should use synchronized keyword or locked on this
- similar synchronizedcollection() wrapper. means should able observe externally.
to continue example of collections.synchronizedcollection() if have 1 thread calling iterator(), should still able methods add() thread because iterator() should not locking. on other hand, should able synchronize on collection externally , see thread blocks on add().
is there way test method synchronized in junit test? want avoid long sleep statements.
if want check if method has synchronized
modifier, aside obvious (looking @ source code/javadoc), can use reflection.
modifier.issynchronized(method.getmodifiers())
the more general question of testing if method guarantees proper synchronization in concurrency scenarios undecidable problem.
Comments
Post a Comment