/**
* Cache Object Map
*/
public static cachepool<string, object> Mappool = Cachepool.getinstance ();
private static final int nthreads=5;
Use the thread pool to avoid creating a single thread for each request.
private static final Executor Threadpool=executors.newfixedthreadpool (nthreads);
Public Financesvo buyproduct (Financesvo financesvo) {
Mappool.put ("Financesvo", FINANCESVO);
String key = "";
Map param = new hashmap<string,string> ();
Synchronized (Mappool) {
System.out.println ("Thread.CurrentThread (). GetName () ..... ............ ..." + "+" "+" "+" "+" Thread.CurrentThread (). GetName ());
Iterator it = Mappool.keyset (). Iterator ();
When the cache is not empty, take out a value
while (It.hasnext ())
{
Key = (String) it.next ();
FINANCESVO = (FINANCESVO) mappool.get (key);
Param.put (key, FINANCESVO);
if (null! = param) {
String resultcode= "";
Financesvo=sureproduct (FINANCESVO);
if (Financesvo.getistrue ()) {
Resultcode= "Success";
}
To prevent duplication, remove it
Mappool.remove (key);
}
}
}
return FINANCESVO;
}
public class Cachepool<key, value> extends Abstractmap<key, value>{
Privatized Cache Object Instances
private static Cachepool Cachepool = new Cachepool ();
private int maxCount = 1000;
Private blockingqueue<entry> queue = new linkedblockingqueue<entry> ();
/**
* Private Constructor.
* @return
*/
Private Cachepool () {
}
/**
* Open a public method to determine if an instance exists, there is a return, there is no new one in return
* @return
*/
public static Cachepool getinstance () {
return cachepool;
}
/**
* The Entry for this Map.
* @author Ancan
*
*/
Private class Entry implements Map.entry<key, value>{
Private key key;
Private value value;
Public Entry (key key, value value) {
This.key = key;
This.value = value;
}
@Override
Public String toString () {
Return key + "=" + value;
}
Public Key GetKey () {
Return key;
}
Public Value GetValue () {
return value;
}
Public value SetValue (value value) {
return this.value = value;
}
}
/**
* Constructor.
* @param size the size of the pooled map;
*/
public cachepool (int size) {
MaxCount = size;
}
@Override
Public Value put (key key, value value) {
while (Queue.size () >= maxCount) {
Queue.remove ();
}
Queue.add (New Entry (key, value));
return value;
}
@Override
Public Value get (Object key) {
for (iterator<entry> iter = Queue.iterator (); Iter.hasnext ();) {
Entry type = Iter.next ();
if (Type.key.equals (key)) {
Queue.remove (type);
Queue.add (type);
return type.value;
}
}
return null;
}
@Override
Public Set<map.entry<key, Value>> EntrySet () {
Set<map.entry<key, value>> set = new Hashset<map.entry<key, value>> ();
Set.addall (queue);
return set;
}
@Override
public void Clear () {
Queue.clear ();
}
@Override
Public set<key> KeySet () {
set<key> set = new hashset<key> ();
for (Entry e:queue) {
Set.add (E.getkey ());
}
return set;
}
@Override
Public Value Remove (Object obj) {
for (Entry e:queue) {
if (E.getkey (). Equals (obj)) {
Queue.remove (e);
return E.getvalue ();
}
}
return null;
}
@Override
public int size () {
return Queue.size ();
}
}
Java thread pool handles concurrent business