Java thread pool handles concurrent business

Source: Internet
Author: User

/**
* 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

Related Article

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.