Tuesday, January 27, 2015

The private lock idiom

Why to use a separate monitor object?

"never synchronize on an variable/field that can change its value ... create some arbitrary private static final Object lock = new Object() and synchronize on that" - John Vint http://stackoverflow.com/questions/3002251/what-is-the-correct-way-to-synchronize-a-shared-static-object-in-java?rq=1

More:
https://www.securecoding.cert.org/confluence/display/java/LCK01-J.+Do+not+synchronize+on+objects+that+may+be+reused
https://www.securecoding.cert.org/confluence/display/java/LCK00-J.+Use+private+final+lock+objects+to+synchronize+classes+that+may+interact+with+untrusted+code

No comments:

Post a Comment