Created
June 6, 2018 11:06
-
-
Save rozag/36cb7fa656d3bb32d5d53e64925dc43c to your computer and use it in GitHub Desktop.
An example of object pool implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Pair { | |
public int firstValue; | |
public int secondValue; | |
// Reference to next object in the pool | |
private Pair next; | |
// The lock used for synchronization | |
private static final Object sPoolSync = new Object(); | |
// The first available object in the pool | |
private static Pair sPool; | |
private static int sPoolSize = 0; | |
private static final int MAX_POOL_SIZE = 50; | |
/** | |
* Only allow new objects from obtain() | |
*/ | |
private Pair() {} | |
/** | |
* Return recycled object or new if pool is empty | |
*/ | |
public static Pair obtain() { | |
synchronized(sPoolSync) { | |
if (sPool != null) { | |
Pair m = sPool; | |
sPool = m.next; | |
m.next = null; | |
sPoolSize--; | |
return m; | |
} | |
} | |
return new Pair(); | |
} | |
/** | |
* Recycle this object. You must release all references to | |
* this instance after calling this method. | |
*/ | |
public void recycle() { | |
synchronized(sPoolSync) { | |
if (sPoolSize < MAX_POOL_SIZE) { | |
next = sPool; | |
sPool = this; | |
sPoolSize++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment