Emulate Android's pooling technology

Source: Internet
Author: User

/** * Emulate Android Pooling technology * @author Fgtian * */public class Objectcachetest {public static class Objectitem {private static int Spoolsize = 0;private static final int max_cache = 10;private static final Object spoollock = new Object ();p rivate static  Objectitem SPool = null;private Objectitem mnext = null;private int mvalue;public static Objectitem obtain () {synchronized (Spoollock) {if (null! = SPool) {Objectitem item = Spool;spool = Item.mnext;item.mnext = Null;--spoolsize;return item;}} return new Objectitem ();} public static Objectitem obtain (int value) {synchronized (Spoollock) {if (null! = SPool) {Objectitem item = Spool;spool = Item.mnext;item.mnext = Null;--spoolsize;item.mvalue = Value;return item;}} return new Objectitem (value);} public void Recycle () {synchronized (Spoollock) {if (Spoolsize < Max_cache) {Mvalue = 0;this.mnext = Spool;spool = This ; spoolsize++;}}} Public Objectitem () {}public objectitem (int value) {mvalue = value;} @Overridepublic String toString () {return string.valueof (Mvalue);}} public static final void main (string[] args) {Objectitem item1 = Objectitem.obtain (1); item1.recycle (); Objectitem item2 = O Bjectitem.obtain (3); if (item1 = = item2) {System.out.println ("YES, use the same OBJECT");} else {System.out.println (" ERROR ");}}}

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.