Ehcache (--ehcache) support for concurrency

Source: Internet
Author: User

http://haohaoxuexi.iteye.com/blog/2119733

Ehcache Support for concurrency

In the case of high concurrency, when using the Ehcache cache, the data we read may be wrong due to concurrent reads and writes, and the data we write may be accidentally overwritten. Fortunately, Ehcache provides us with a read (read), write-lock for the cache element key. When a thread acquires a read lock on a key, the other thread acquires a read lock on the same key without restriction, but the other thread (including the thread that acquires the read lock on the key) does not want to get a write lock on the same key. It needs to wait until the read lock on the key is released before it can get its write lock, and when a thread acquires a write lock on a key, Requests from other threads for a read or write lock on the same key will wait for the write lock on the key to be released before proceeding, but the same thread gets the read lock or write lock corresponding to the key without waiting. After acquiring the corresponding lock, remember to release the lock after the lock is no longer needed. And be careful not to cause deadlocks.

 Several methods related to read and write locks are defined for us in the Ehcache interface, as follows: public interface Ehcache {/** * Gets the read lock for a given key * @param         Key */public void Acquirereadlockonkey (Object key);         /** * Gets the write lock for the given key * @param key */public void Acquirewritelockonkey (Object key);      /** * Attempts to obtain a read lock on a given key and returns False if the corresponding read lock has not been acquired within the given timeout time, otherwise true.      * @param key * @param timeout time-out, in milliseconds * @return indicates if the corresponding read lock was obtained * @throws interruptedexception */         public boolean Tryreadlockonkey (Object key, long timeout) throws interruptedexception;      /** * Attempts to get the write lock for a given key, returns False if the corresponding write lock has not been acquired within the given timeout time, otherwise true.      * @param key * @param timeout time-out, in milliseconds * @return indicates if the corresponding write lock was obtained * @throws interruptedexception */         public boolean Trywritelockonkey (Object key, long timeout) throws interruptedexception; /** * Release the read lock for the given key held * @param key */public void ReleasereadlOckonkey (Object key);      /** * Release the Write lock for the given key held * @param key */public void Releasewritelockonkey (Object key);   }

our commonly used cache classes have implemented these methods for us, and we can use them directly in the program. The following is a simple example of using a lock directly in your program.

@Test public  void Test () {     CacheManager CacheManager = Cachemanager.create ();     Cachemanager.addcache ("test");     Cache cache = Cachemanager.getcache ("test");     Final String key = "abc";     Cache.acquirewritelockonkey (key);     try {        cache.put (key, "123"));     } finally {        System.out.println (Cache.get (key));        Cache.releasewritelockonkey (key);     }  }


Remember to release the acquired lock at the right time.

(Note: This article is written based on ehcache2.8.1)

Ehcache (--ehcache) support for concurrency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.