In this post under Apache Pool, I will show with example how to create a synchronized object pool factory.
Below is the complete main class for your reference
Main class
package package17;
import org.apache.commons.pool2.PoolUtils;
import org.apache.commons.pool2.PooledObjectFactory;
public class StringBufferPoolExample17 {
public static void main(String[] args) {
StringBufferPooledObjectFactory stringBufferPooledObjectFactory = new StringBufferPooledObjectFactory();
PooledObjectFactory<StringBuffer> threadSafePooledObjectFactory = PoolUtils.synchronizedPooledFactory(stringBufferPooledObjectFactory);
System.out.println(threadSafePooledObjectFactory);
}
}
In the above code, at line 8, I create an instance of “StringBufferePooledObjectFactory”.
At line 9 I create an instance of synchronized pool factory from the instance created at line 8 and using “PoolUtils” “synchronizedPooledFactory” method.
I pass the instance created at line 8 as an argument to “PoolUtils” “synchronizedPooledFactory” method.
In this way we can create a synchronized object pool factory.