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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -