- Package com.jd.test;
- Import java.io.Serializable;
- Import Java.util.LinkedHashMap;
- Import Java.util.concurrent.locks.Lock;
- Import Java.util.concurrent.locks.ReentrantLock;
- /**
- * Cache Class (least recently unused)
- *
- * @author Liuzhenfeng
- *
- * @param <K,V>
- */
- Public class Lrucache<k, v> extends Linkedhashmap<k, v> implements Serializable {
- /**
- * Cache Default Size
- */
- public static final int default_capasity = 20;
- /**
- * Cache Actual Size
- */
- public static int cache_capasity = default_capasity;
- /**
- * Thread Sync Lock
- */
- private static final lock lock = new Reentrantlock ();
- Public LRUCache () {
- super (default_capasity);
- cache_capasity = default_capasity;
- }
- Public LRUCache (int size) {
- super (size);
- cache_capasity = size;
- }
- /*
- * Clear Cache
- *
- * @see Java.util.linkedhashmap#clear ()
- */
- @Override
- public void Clear () {
- try {
- Lock.lock ();
- super.clear ();
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Determine if the object is included
- *
- * @see Java.util.linkedhashmap#containsvalue (java.lang.Object)
- */
- @Override
- Public Boolean containsvalue (Object value) {
- try {
- Lock.lock ();
- return super.containsvalue (value);
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Querying objects from the cache
- *
- * @see Java.util.linkedhashmap#get (java.lang.Object)
- */
- @Override
- Public V Get (Object key) {
- try {
- Lock.lock ();
- return super.get (key);
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Whether to delete the oldest unused cache object
- *
- * @see Java.util.linkedhashmap#removeeldestentry (java.util.Map.Entry)
- */
- @Override
- protected Boolean removeeldestentry (java.util.map.entry<k, v> eldest) {
- try {
- Lock.lock ();
- return this.size () > cache_capasity;
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Determine if the key is included in the cache
- *
- * @see Java.util.hashmap#containskey (java.lang.Object)
- */
- @Override
- Public Boolean containskey (Object key) {
- try {
- Lock.lock ();
- return Super.containskey (key);
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Determine if the cache is empty
- *
- * @see Java.util.hashmap#isempty ()
- */
- @Override
- Public Boolean isEmpty () {
- try {
- Lock.lock ();
- return super.isempty ();
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Put in cache
- *
- * @see java.util.hashmap#put (java.lang.Object, Java.lang.Object)
- */
- @Override
- Public V put (K key, V value) {
- try {
- Lock.lock ();
- return super.put (key, value);
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Removed from cache
- *
- * @see Java.util.hashmap#remove (java.lang.Object)
- */
- @Override
- Public V Remove (Object key) {
- try {
- Lock.lock ();
- return super.remove (key);
- } finally {
- Lock.unlock ();
- }
- }
- /*
- * Cache Size
- *
- * @see Java.util.hashmap#size ()
- */
- @Override
- public int size () {
- try {
- Lock.lock ();
- return super.size ();
- } finally {
- Lock.unlock ();
- }
- }
- }
The re-entry lock (Reentrantlock) is a recursive, non-blocking synchronization mechanism. It was previously thought to be a simple alternative to synchronized, and the implementation mechanism was not too far apart. But the recent practice has found a difference between them.
Here is the official note: a reentrant mutex lock lock, which has the same basic behavior and semantics as the implicit monitor lock accessed using the Synchronized method and the statement, but is more powerful. Reentrantlock will be owned by a thread that has recently successfully acquired a lock and has not released the lock. When the lock is not owned by another thread, the thread that called lock succeeds in acquiring the lock and returns. If the current thread already owns the lock, this method will return immediately. You can use the Isheldbycurrentthread () and Getholdcount () methods to check whether this condition occurs.
It provides the lock () method:
If the lock is not persisted by another thread, gets the lock and returns immediately, setting the locked hold count to 1.
If the current thread has already held the lock, the count will be added to 1, and the method returns immediately.
If the lock is persisted by another thread, the current thread is disabled for the purpose of thread scheduling, and the thread will remain dormant until the lock is acquired, at which time the lock hold count is set to 1.
Java implements LRU (least recently used) cache