Created
April 12, 2011 15:48
-
-
Save taichi/915770 to your computer and use it in GitHub Desktop.
commons-pool
This file contains 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
import org.apache.commons.pool2.PoolableObjectFactory; | |
import org.apache.commons.pool2.impl.GenericObjectPool; | |
public class PoolTest { | |
public static void main(String[] args) throws Exception { | |
GenericObjectPool<PooledObject> pool = new GenericObjectPool<PoolTest.PooledObject>( | |
new F()); | |
pool.addObject(); | |
pool.addObject(); | |
System.out.println("======"); | |
PooledObject p1 = pool.borrowObject(); | |
PooledObject p2 = pool.borrowObject(); | |
System.out.println(p1); | |
System.out.println(p2); | |
pool.returnObject(p1); | |
pool.returnObject(p2); | |
pool.close(); | |
} | |
static class PooledObject { | |
public PooledObject() { | |
System.out.println("created"); | |
} | |
void close() { | |
System.out.println("closed"); | |
} | |
} | |
static class F implements PoolableObjectFactory<PooledObject> { | |
@Override | |
public PooledObject makeObject() throws Exception { | |
return new PooledObject(); | |
} | |
@Override | |
public void destroyObject(PooledObject obj) throws Exception { | |
obj.close(); | |
} | |
@Override | |
public boolean validateObject(PooledObject obj) { | |
System.out.println("validateObject"); | |
return true; | |
} | |
@Override | |
public void activateObject(PooledObject obj) throws Exception { | |
System.out.println("activate"); | |
} | |
@Override | |
public void passivateObject(PooledObject obj) throws Exception { | |
System.out.println("passive"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment