Objectpool in Java

Source: Internet
Author: User

Recently, in the collision detection module of the Editor (Java + jogl), the detection function requires a large number of temporary objects, such as vector3f and matrix4f. Java is inherently slow, if we use new without scruples, I am really sorry... Fortunately, I found the objectpool concept, which is simple but practical.

 

/** <Br/> * @ author Yong. xue <br/> */<br/> public class ipoolvec3 {<br/> Private Static arraylist <vector3f> objects = new arraylist <vector3f> (256 ); <br/> Private Static int n = 256; <br/> Public static vector3f alloc (float X, float y, float Z) {<br/> If (n> 0) {<br/> vector3f v = objects. remove (-- N); <br/> v. set (x, y, z); <br/> return V; <br/>}else {<br/> return New vector3f (); <br/>}< br/> Public static vector3f alloc () {<br/> If (n> 0) {<br/> vector3f v = objects. remove (-- N); <br/> v. zero (); </P> <p> return V; <br/>} else {<br/> return New vector3f (); <br/>}< br/> Public static void free (vector3f v) {<br/> If (V = NULL) {<br/> return; <br/>}< br/> objects. add (V); <br/> + + N; <br/>}< br/>}

 

 

The usage is as follows:

/** <Br/> * tests the triangle for intersection with a ray. <br/> * @ Param Ray <br/> * @ return the distance between the ray origin and the intersection point <br/> */<br/> public float intersect (iray ray) {<br/> // map TMP vectors to local names <br/> vector3f e1 = ipoolvec3.alloc (); <br/> vector3f e2 = ipoolvec3.alloc (); <br/> vector3f P = ipoolvec3.alloc (); <br/> vector3f q = ipoolvec3.alloc (); <br/> vector3f S = ipoolvec3.alloc (); <br/> try {<br/> // test raydirection <br/> e1.sub (m_pvpositions [1], m_pvpositions [0]); <br/> e2.sub (m_pvpositions [2], m_pvpositions [0]); <br/> P. cross (Ray. m_vdirection, E2); <br/> float a = e1.dot (p); <br/> // if the result is "zero ", the pick Ray is parallel to the triangle <br/> If (-imath. epsilon <A) & (A <imath. epsilon) {<br/> return (-1f); <br/>}< br/> float F = 1.0f/; <br/> // compute barycentric coordinates <br/> S. sub (Ray. m_vorigin, m_pvpositions [0]); <br/> final float u = f * s. DOT (p); <br/> If (0.0f> U) | (u> 1.0f) {<br/> return (-1f ); <br/>}< br/> q. cross (S, E1); <br/> final float v = f * Ray. m_vdirection.dot (Q); <br/> If (0.0f> U) | (U + V> 1.0f) {<br/> return (-1f ); <br/>}< br/> // compute the intersection point on the ray <br/> final float L = f * e2.dot (Q ); <br/> If (L <0.0f) {<br/> return (-1f); <br/>}< br/> return (L * l ); <br/>} finally {<br/> ipoolvec3.free (s); <br/> ipoolvec3.free (Q); <br/> ipoolvec3.free (P ); <br/> ipoolvec3.free (E2); <br/> ipoolvec3.free (E1); <br/>}< br/>}

 

Well, all the temporary objects are not new, but get a reference from the object pool; if the pool does not exist, the new operation is implemented;

The most important thing is that it must be returned after use. Otherwise, memory leakage similar to C/C ++ may occur, with fewer and fewer available elements in the pool ..

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.