Concept of the javaweb framework-its own object storage pool and the concept of the javaweb framework

Source: Internet
Author: User

Concept of the javaweb framework-its own object storage pool and the concept of the javaweb framework


Original Design:
When a website provides services, many objects such as bean and dao are created frequently. However, these objects can be reused.
Design Concept:
The ObjectPool of the object connection pool uses the single-State design mode. It comes with a thread and calls
ClearObject method,
To ensure synchronization, the synchronized keyword is used for all methods involving thread security.
The process control of each method depends on the status of the stored value and the data that affects the status of the stored value.
The class to be stored in the ObjectPool must implement the AllowEnterObjectPool interface.
Main attributes:
HashMap <String, ArrayList> hashMapForStorageObject, the key is the package name + class name, and the value stores the instance of the modified class. Every time it is retrieved and put back, it will be taken from the 0 position.
HashMap <String, Integer> hashMapForObjectUsedTime key is the package name + class name + @ + object hashcode Integer, which records the number of retrieved objects.
Main Methods:
The takeOut method uses the generic type and only needs to pass it to the Class to return the corresponding type. The reflection is used to add objects in batches.
Put indicates that the object is restored to the initial state (specified by the programmer) and put to the 0 position of the corresponding ArrayList.
ClearObject, called by Internal Threads, traverses each ArrayList of hashMapForStorageObject. If the total number of arraylists is greater than the minimum number of storage
AshMapForObjectUsedTime: query the usage record, delete the objects that meet the condition, and clear the usage record.
Code and usage:

Package myFrame; public interface AllowEnterObjectPool {public ObjectPool objectPool = ObjectPool. getInstance (); // when you put an object into the class change, you need to call the change method to restore the object to the default state public void initialMember (); // when you call the takeOut method, if no available objects are found, the system will call the modification method to add public void batchAddSelf ();}

 

Package myFrame; import java. lang. reflect. method; import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. linkedHashSet; import com. sun. security. auth. NTDomainPrincipal; import Dao. testDao; public class ObjectPool implements Debug, Assistant {// when only 30 objects are left, private int minNumber = 30 will not be deleted; // check the interval at which private int checkTime = 60*60*1000; // each class corresponds to an ArrayList // the key value is package name + class name. // The value of value is the Arraylist that stores the object // The value of value that affects the program flow is null (No find key) 0 (size) // minNumber private HashMap <String, ArrayList> hashMapForStorageObject = new HashMap <String, ArrayList> (); // each object corresponds to an Integer, record the number of calls within a period of time/** the key value has been included in the package name + class name + @ + object hashcode value records the number of times this object has been taken out value affects the value of the program flow null (key not found)> 0 (used) */private HashMap <String, Integer> hashMapForObjectUsedTime = new HashMap <String, Integer> (); private static ObjectPool objectPool = new ObjectPool (); private ObjectPool () {new clock (). start () ;}// get an instance of the Class, get it from 0 each time, and delete public <T> T takeOut (Class <T> object) from ArrayList) {synchronized (this) {String className = object. getName (); ArrayList <T> objectList = hashMapForStorageObject. get (className); if (objectList = null) {objectList = new ArrayList <T> (); hashMapForStorageObject. put (className, objectList); T createObject = null; try {createObject = object. newInstance (); Method batchAddSelf = object. getMethod ("batchAddSelf"); batchAddSelf. invoke (createObject); objectList. add (createObject);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}} else if (objectList. size () = 0) {T createObject = null; try {createObject = object. newInstance (); Method batchAddSelf = object. getMethod ("batchAddSelf"); batchAddSelf. invoke (createObject); objectList. add (createObject);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}t returnObject = objectList. get (0); String flag = className + "@" + Integer. toHexString (returnObject. hashCode (); Integer I = hashMapForObjectUsedTime. get (flag); if (I = null) {I = 0;} hashMapForObjectUsedTime. put (flag, ++ I); objectList. remove (returnObject); return returnObject;}/** after the object is restored to the initial state, put it in the 0 position */public synchronized void put (AllowEnterObjectPool allowEnterObjectPool) {allowEnterObjectPool. initialMember (); String className = allowEnterObjectPool. getClass (). getName (); ArrayList objectList = hashMapForStorageObject. get (className); if (objectList = null) {objectList = new ArrayList (); hashMapForStorageObject. put (className, objectList);} objectList. add (0, allowEnterObjectPool);}/** the hashMapForStorageObject prevails. Retrieve an ArrayList A and traverse the objects in it, for example,, key value that makes up hashMapForObjectUsedTime * when the capacity of A is greater than minnumber, use the key to find hashMapForObjectUsedTime. If the value is null, delete the corresponding object * In A and clear the hashMapForObjectUsedTime data */public synchronized void clearObject () {Iterator <String> iterator = hashMapForStorageObject. keySet (). iterator (); String key, flag; ArrayList list; while (iterator. hasNext () {key = iterator. next (); list = hashMapForStorageObject. get (key); Object object; Integer usedTime; if (list. size () <minNumber) {continue;} for (int I = 0; I <list. size (); I ++) {object = list. get (I); flag = key + "@" + Integer. toHexString (object. hashCode (); usedTime = hashMapForObjectUsedTime. get (flag); if (usedTime = null) {list. remove (I); hashMapForObjectUsedTime. remove (key); I --; if (list. size () = minNumber) break ;}} hashMapForObjectUsedTime. clear ();} public static ObjectPool getInstance () {return objectPool;} private class clock extends Thread {@ Override public void run () {System. out. println ("clock start"); while (true) {try {Thread. sleep (checkTime);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}clearobject () ;}} public static void main (String [] args) {Message message = objectPool. takeOut (Message. class); objectPool. put (message); System. out. println (message );}}

 

package myFrame;public class Message implements AllowEnterObjectPool{    private String name;    private int age;    public Message()    {            }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public void initialMember() {        // TODO Auto-generated method stub        name = "";        age = 0;    }    @Override    public void batchAddSelf()     {        Message message;        for(int i=0;i<10;i++)        {            message = new Message();            objectPool.put(message);        }    }    }

 

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.