{"id":16201,"date":"2014-12-02T18:14:45","date_gmt":"2014-12-02T12:44:45","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16201"},"modified":"2014-12-02T18:32:16","modified_gmt":"2014-12-02T13:02:16","slug":"groovy-goodness-readwritelocks","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-goodness-readwritelocks\/","title":{"rendered":"Groovy Goodness &#8211; ReadWriteLocks"},"content":{"rendered":"<p>We often use synchronization. Imagine you have a use case in which there is some resource upon which writing is not done as much as compared to reading.\u00a0So multiple threads should be able to read concurrently same resource without any problems. But if a single thread wants to write to the resource,\u00a0for that all new threads for reading should be blocked or allowed to finish execution if already in reading process.<\/p>\n<p>In other words while writing no other threads should\u00a0be able\u00a0read or write, this can be achieved using read \/ write lock. In Java one can use an implementation of ReadWriteLock interface in java.util.concurrent.locks package which provides readLock and writeLock objects to achieve this.<\/p>\n<p>Groovy has provided two annotations for above use which are simply wrapper of ReentrantReadWriteLock<br \/>\n&#8211; groovy.transform.WithReadLock<br \/>\n&#8211; groovy.transform.WithWriteLock<\/p>\n<p>import groovy.transform.*;<\/p>\n<p>We will see an example to understand. So below getResource() method is not blocked until any thread is\u00a0accessing any of updateAndGetResource() and refresh() methods and vice versa.<\/p>\n<p>[java]<\/p>\n<p>public class ResourceProvider {<br \/>\nprivate final Map&lt;String, Object&gt; data = new HashMap&lt;String, Object&gt;();<\/p>\n<p>@WithReadLock<br \/>\npublic String getResource(String key) throws Exception {<br \/>\nreturn data.get(key);<br \/>\n}<\/p>\n<p>@WithWriteLock<br \/>\npublic void refresh() throws Exception {<br \/>\n\/\/reload the resources into memory<br \/>\n}<\/p>\n<p>@WithWriteLock<br \/>\npublic String updateAndGetResource(String key){<br \/>\nrefresh()<br \/>\n\/\/updating the shared resource for some special key<br \/>\ngetResource(key)<br \/>\n}<\/p>\n<p>\/\/no blocking required<br \/>\npublic void update(String key){<br \/>\nObject object = data.get(key)<br \/>\n\/\/now update object mutable attributes<br \/>\n}<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>Now you can use synchronization much more efficiently with read\/write locks.<\/p>\n<p>Hope this helps!!!<\/p>\n<p>Parampreet Singh<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We often use synchronization. Imagine you have a use case in which there is some resource upon which writing is not done as much as compared to reading.\u00a0So multiple threads should be able to read concurrently same resource without any problems. But if a single thread wants to write to the resource,\u00a0for that all new [&hellip;]<\/p>\n","protected":false},"author":142,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[1557,9,1559,1558],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16201"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/142"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16201"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16201\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}