Home > Back-end >  I use synchronized and already right?
I use synchronized and already right?

Time:09-17

This is the basic frame of my code:


Here is my code:
 
package com.test;

Public class Object1 {
Private String s1.
Private String s2.
Public String getS1 () {
Return s1.
}
Public void setS1 (String s1) {
This. S1=s1;
}
Public String getS2 () {
Return s2.
}
Public void setS2 (String s2) {
This. S2=s2;
}
}

 
package com.test;

Public class Object2 {
Private int i1.
Private int i2.
Public int getI1 () {
Return i1.
}
Public void setI1 (int i1) {
This. I1=i1;
}
Public int getI2 () {
Return i2.
}
Public void setI2 (int i2) {
This. I2=i2;
}
}

 
package com.test;

Public class Engine {
Private Cache Cache=new Cache ();
Private static Engine instance=null;

/* *
* single instance mode
*/
Private Engine () {

}

Public Cache getCache () {
Return the cache;
}

Public static Engine getInstance () {
Synchronized (Engine. Class) {
If (the instance==null) {
The instance=new Engine ();
}
}
Return the instance.
}
}

 
package com.test;

Import the Java. Util. Concurrent. The locks. *;

Public class HandleLock {
Private String handle;
Private already lock;

Public HandleLock () {
The lock=new already ();
}

Public String getHandle () {
Return the handle.
}
Public void setHandle (String handle) {
This. Handle=handle;
}
The public already getLock () {
Return the lock;
}
Public void setLock (already lock) {
The lock=lock; this.
}
}

 
package com.test;

Import the Java. Util. HashMap;
import java.util.LinkedList;
Import the Java. Util. The Map;

/* *
* Just as the class name are, this is a cache.
*
*
*/
Public class Cache {
/* *
* map1 and map2 are 2 store the actual useful information.
* The key of map1 and map2 is CacheInfo. GenKey ()
*/
Private MapPrivate Map
/* *
* store the CacheInfo object in this order: the first is the most recently, informs,
* and the last is the further recently, informs. For example, the first is 2 in 17:03,
* the second is 2 in 17:01, the third is 2 in 17:00, and so on.
* So the CacheInfo object in the end are more likely to be removed when the cache is full.
* That the cache is full means the size of the link is larger than cacheSize (the next parameter in this class).
* Every CacheInfo object in the link has its information stored in map1 and map2, i.e. for
* they are corresponding to each other. And so
* if the link in the size () is 8, map1. The size () and map2. The size () are both 8.
*/
Private LinkedList The link=new LinkedList (a);

Int cacheSize=10;

/* *
* The key of lockMap is CacheInfo. GenKey (), i.e. for is The same as map1 and map2.
* Every CacheInfo object in the link has its corresponding HandleLock in lockMap and of course Every key
* in map1 and map2 has its corresponding HandleLock in lockMap.
*/
Private HashMap LockMap=new HashMap (a);

Public Object1 getObject1 (String s) {
Return map1. Get (s);
}

Public Object2 getObject2 (String s) {
Return map2. Get (s);
}

Public void removeObject1 (String s) {
Map1. Remove (s);
}

Public void removeObject2 (String s) {
Map2. Remove (s);
}

Public synchronized void removeFromLink (CacheInfo CacheInfo) {
This. The link. Remove (cacheInfo);
}

Public synchronized Boolean isExist (CacheInfo CacheInfo) {
Return the link. The contains (cacheInfo);
}

Public synchronized Boolean needRemove () {
Return the link. The size () & gt;=cacheSize;
}

Public synchronized void addToHead (CacheInfo CacheInfo) {
The link. AddFirst (cacheInfo);
}

Public synchronized void adjustToHead (CacheInfo CacheInfo) {
CacheInfo head=link. The getFirst ();
If (head. Equals (cacheInfo)==false) {
The link. Remove (cacheInfo);
The link. AddFirst (cacheInfo);
}
}

Public synchronized HandleLock getLock (String cacheInfoKey) {
The lock=HandleLock lockMap. Get (cacheInfoKey);
If (lock==null) {
The lock=new HandleLock ();
The lock. SetHandle (cacheInfoKey);
LockMap. Put (cacheInfoKey, lock);
}
Return the lock;
}

/* *
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related