Recently, some functional jar packages have been encapsulated. Because some actions need to be generated and some callbacks need to be provided to the caller, events and listeners are used.
For example, in draglistener and dragevent, each drag action triggers a dragevent event at the beginning, and a new dragevent object is required. Later, I felt like this was a waste of memory. Then I studied the motionevent class of the system and found the correct one.
Solution.
The motionevent construction method is anonymous and cannot be directly created. The interface for obtaining objects externally is a static obtain method, which can be obtained from a motionevent object or some variables. Why is it a good solution, because it provides a recycle Method
You can recycle the current object. You don't need to create a new object next time, just take it from its recycle pool.
The following describes several important variables in motionevent:
Java code
- // Variable
- Private motionevent mnext;
// Point to the next object of the recycle Stack
- Private Boolean mrecycled;
// Indicates whether the object is recycled.
- // Static variables
- Static private
Final int max_recycled =
10; // The maximum number of recyclables
- Static private object gresponerlock =
New object (); // lock
- Static private
Int gresponerused = 0; // Number of recycled objects in the recycle Stack
- Static private motionevent grecyclertop =
NULL; // reclaim the stack top object of the stack
// Variable private motionevent mnext; // point to the next object of the recycle stack private Boolean mrecycled; // indicates whether the object is recycled/static variable static private final int max_recycled = 10; // maximum number of recyclables static private object gresponerlock = new object (); // The static private int gresponerused = 0 used to lock the entire class; // reclaim the number of objects in the stack. Static private motionevent gresponertop = NULL; // reclaim the top object of the stack.
Then there is a static obtain method:
Java code
- Static private motionevent obtain (){
- Synchronized (gresponerlock ){
// Lock the entire class
- If (gresponertop =
Null) {// If the stack top does not exist, a new
- Return
New motionevent ();
- }
- Motionevent EV = gresponertop; // The stack top exists, and a reference eV is used to point to it.
- Gresponertop = eV. mnext; // then, the next object at the top of the stack is raised to the top of the stack.
- Gresponerused --; // reduce the number of objects in the recycle stack by one
- Ev. mrecycledlocation = NULL; // an exception with unknown function
- Ev. mrecycled = false; // The current object flag is not recycled.
- Return EV;
- }
- }
Static private motionevent obtain () {synchronized (gresponerlock) {// lock the entire class if (gresponertop = NULL) {// The stack top does not exist, A new return New motionevent ();} motionevent EV = gresponertop; // The stack top exists, and a reference eV is used to point to it gresponertop = eV. mnext; // then the next object at the top of the stack is mentioned to the gresponerused -- on the top of the stack; // The number of objects in the recycle stack is reduced by one eV. mrecycledlocation = NULL; // It is an exception that affects unknown eV. mrecycled = false; // the flag of the current object is return EV ;}}
The other several obtain Methods call the obtain () method to retrieve objects from the recycle stack and assign values.
Its recycle method is as follows:
Java code
- Public void recycle (){
- // Ensure that the recycle method is called only once
- If (track_recycled_location ){
- If (mrecycledlocation! =
Null ){
- Throw
New runtimeexception (tostring () + "recycled twice! ", Mrecycledlocation );
- }
- Mrecycledlocation = new runtimeexception ("Last recycled here ");
- } Else if (mrecycled ){
- Throw new runtimeexception (tostring () +
"Recycled twice! ");
- }
- Synchronized (gresponerlock) {// lock class
- If (gresponerused <max_recycled ){
// If the objects in the recycle stack have not reached the maximum value
- Gresponerused ++; // The number of elements in the recycle stack increases by 1.
- Mnumhistory = 0;
- // These two statements point the next of the current object to the top of the previous stack, and then place the current object to the top of the stack.
- Mnext = gresponertop;
- Gresponertop = this;
- }
- }
- }
Public void recycle () {// make sure that the recycle method only calls if (track_recycled_location) {If (mrecycledlocation! = NULL) {Throw new runtimeexception (tostring () + "recycled twice! ", Mrecycledlocation);} mrecycledlocation = new runtimeexception (" Last recycled here ");} else if (mrecycled) {Throw new runtimeexception (tostring () +" recycled twice! ");} Synchronized (grecyclerlock) {// lock class if (grecyclerused <max_recycled) {// if the objects in the recycle stack have not reached the maximum value grecyclerused ++; // increase the number of elements in the recycle stack by 1 mnumhistory = 0; // the two statements point the next of the current object to the top of the previous stack, then place the current object to the top of the stack. mnext = gresponertop; gresponertop = This ;}}}
Based on this idea, I also made an event. The same recovery principle greatly saves the memory usage when the event is triggered frequently.