In practice, we often encounter areas where pooling is needed, especially database connection pooling.
Why do we need a pool? Because these resources are created, resources are consumed. Therefore, we use an object pool, which is pre-created with some resource objects. When we need to, we pull the object out of the pool, and when we don't need it, we return the object to the pool. This can improve the efficiency of your code's operation.
Apache Commons Pool (http://commons.apache.org/pool/) provides a convenient interface for us to implement object pooling. The only thing we need to achieve is how to produce objects without having to think about a bunch of multithreaded problems.
In 2013, Apache Commons Pool 2.0 was released, an implementation of a fully rewritten object pool that significantly improved performance and scalability, especially in the case of high concurrent loads. Version 2.0 contains reliable instance tracking and pool monitoring, requiring JDK 1.6 or later. This version is completely incompatible with the 1.x version;
Apache Commons Pool from a personal point of view, there are three key points
1.Factory
Callback. Used for object creation, destruction, validation, blockage prevention, etc.
Configuration of 2.Pool
The parameters of the pool. For example, the maximum number of, the minimum number, the longest blockage time and so on.
3.Pool instances
The actual work.
Apachecommons Pool 1.x Sample code:
Package Test.ffm83.commons.pool;
importJava.text.SimpleDateFormat;
importjava.util.Date;
importorg.apache.commons.pool.BasePoolableObjectFactory;
importOrg.apache.commons.pool.impl.GenericObjectPool;
/**
*commons Pool simple and practical, based on the 1.x version
* 1.Factory Testpoolableobjectfactory
* 2.Pool the configuration is the pool
* 3.Pool Example Resource
* @author Fan Fangming
*/
Public class Poolbaseusage {
Public static void main (string[] args) {
Final genericobjectpool pool = new Genericobjectpool (
New testpoolableobjectfactory ());
Pool.setmaxactive (2);
for (int i = 0; i < 6; i++) {
New Thread (new Runnable () {
@Override
Public voidrun () {
Try {
Objectobj = Pool.borrowobject (); // Get
System. out. println (obj);
Thread. Sleep (5000);
pool.returnobject (obj); // returned the
}catch (Exception e) {
E.printstacktrace ();
}
}
}). Start ();
}
}
static class Testpoolableobjectfactory extendsbasepoolableobjectfactory{
Public Object Makeobject ()throws Exception {
return new Resource ();
}
}
static class Resource {
Public static int ID;
private int rid;
Public Resource () {
synchronized (this) {
RID = ID+ +;
}
}
Public intGetrid () {
return rid;
}
@Override
Public String toString () {
SIMPLEDATEFORMATDF = newSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); // Set Date format
Stringstr = Df.format (newDate ()) +","+ "ID:" +rid;
return str;
}
}
}
As a result, here are some instructions that set the maximum number of threads to 2 and enable 6 threads to run:
Operation Result:
2014-12-19 14:41:43,id:1
2014-12-19 14:41:43,id:0
2014-12-19 14:41:48,id:0
2014-12-19 14:41:48,id:1
2014-12-19 14:41:53,id:1
2014-12-19 14:41:53,id:0
Looking closely at the results of this operation, you can see that it is clearly divided into 3 waves in the run.
If we adjust the maximum number of threads, the results should change significantly.
Adjust the maximum number of threads to pool.setmaxactive (4);
The results of the operation are as follows:
2014-12-19 15:08:39,id:3
2014-12-19 15:08:39,id:1
2014-12-19 15:08:39,id:2
2014-12-19 15:08:39,id:0
2014-12-19 15:08:44,id:3
2014-12-19 15:08:44,id:1
It can be seen that 4 threads are executed together, and the other two follow-up execution;
Apache Commons Pool Introduction and pool connection pooling code