Java中的ObjectPool

來源:互聯網
上載者:User

最近在寫編輯器(Java+JOGL)的碰撞檢測模組,檢測函數中,需要用到大量的臨時對象,Vector3f, Matrix4f,諸如此類; Java本來就慢,如果我們毫無顧忌的使用 new, 真的是不好意思。。。幸好發現了這個ObjectPool的概念,雖然很簡單,但很實用。

 

/**<br /> *<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 /> }<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 /> }<br /> public static void Free(Vector3f v) {<br /> if(v == null) {<br /> return;<br /> }<br /> objects.add(v);<br /> ++n;<br /> }<br />} 

 

 

使用方法如下

/**<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 / a;<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 /> } 

 

恩,所有的臨時對象都不是new,而是從object pool中取得一個引用;如果pool裡沒有了,才實行new操作;

最重要的一點是,使用完畢後,一定要歸還啊,否則就會造成類似C/C++中的記憶體流失,pool中可用元素越來越少。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.