Java methods for saving data using the HashMap cache _java

Source: Internet
Author: User

The example in this article describes how Java uses the HashMap cache to save data. Share to everyone for your reference, specific as follows:

Private static final Hashmap<long, xxx> scache = new Hashmap<long, xxx> ();
private static int sId =-1;
public static void Initalbumartcache () {
  try {
    //...
    if (id!= sId) {
      clearcache ();
      SId = ID; 
    }
  } catch (RemoteException e) {
    e.printstacktrace ();
  }
}
public static void ClearCache () {
  synchronized (scache) {
    scache.clear ();
  }
}
public static XXX getcachedxxx (Long Index, bitmapdrawable defaultbitmap) {
  XXX d = null;
  Synchronized (Scache) {
    d = scache.get (Index);
  }
  if (d = = null) {
    //...
    synchronized (sartcache) {
      //The cache may have changed since we checked
      XXX value = Scache.get (Index); 
   if (value = = null) {
        scache.put (Index, D);
      } else {
        d = value;
      }
  }} return D;
}

More about Java algorithms Interested readers can view the site topics: "Java data structure and algorithms tutorial", "Java Operation DOM node skills summary", "Java file and directory operation tips Summary" and "Java Cache operation Tips"

I hope this article will help you with Java programming.

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.