Created
July 9, 2013 10:41
-
-
Save GrmpCerber/5956444 to your computer and use it in GitHub Desktop.
pooling test using GenericObjectPool. based on http://stackoverflow.com/a/13914445/233906
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 java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicLong; | |
import org.apache.commons.pool.BasePoolableObjectFactory; | |
import org.apache.commons.pool.PoolableObjectFactory; | |
import org.apache.commons.pool.impl.GenericObjectPool; | |
public class Share { | |
private static final int NB_LOOPS = 100000; | |
final static int NB_THREADS = 100; | |
private static PoolableObjectFactory<MessageDigest> factory = new BasePoolableObjectFactory<MessageDigest>() { | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.apache.commons.pool.BasePoolableObjectFactory#makeObject () | |
*/ | |
@Override | |
public MessageDigest makeObject() throws Exception { | |
return MessageDigest.getInstance("MD5"); | |
} | |
}; | |
final byte[] bytes = new byte[100]; | |
final ExecutorService pool; | |
final static GenericObjectPool<MessageDigest> mdPool = new GenericObjectPool<MessageDigest>( | |
factory); | |
static { | |
mdPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); | |
} | |
static final AtomicLong grandTotal = new AtomicLong(0); | |
Share() throws NoSuchAlgorithmException { | |
pool = Executors.newFixedThreadPool(NB_THREADS); | |
} | |
void go() { | |
for (int i = 0; i < NB_THREADS; i++) { | |
pool.execute(new Runnable() { | |
public void run() { | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < NB_LOOPS; i++) { | |
MessageDigest digest = null; | |
try { | |
digest = mdPool.borrowObject(); | |
digest.update(bytes); | |
digest.digest(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} finally { | |
try { | |
mdPool.returnObject(digest); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
Thread.yield(); | |
}// fin for | |
long end = System.currentTimeMillis(); | |
System.out.println(end - start); | |
grandTotal.addAndGet(end - start); | |
pool.shutdown(); | |
} | |
}); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
startStatThread(); | |
Share share = new Share(); | |
share.go(); | |
share.pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); | |
System.out.println(grandTotal.get() + " ms"); | |
System.out.println(TimeUnit.MILLISECONDS.toSeconds(grandTotal.get()) | |
+ " s"); | |
printPoolStatus(); | |
} | |
/** | |
* | |
*/ | |
private static void startStatThread() { | |
Thread thread = new Thread("PoolStats") { | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.lang.Thread#run() | |
*/ | |
@Override | |
public void run() { | |
while (true) { | |
printPoolStatus(); | |
try { | |
TimeUnit.SECONDS.sleep(1); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
}; | |
thread.setDaemon(true); | |
thread.start(); | |
} | |
/** | |
* | |
*/ | |
private static void printPoolStatus() { | |
int numIdle; | |
int numActive; | |
synchronized (mdPool) { | |
numActive = mdPool.getNumActive(); | |
numIdle = mdPool.getNumIdle(); | |
} | |
System.out.println("active = " + numActive); | |
System.out.println("idle = " + numIdle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment