Created
August 29, 2012 14:51
-
-
Save petejohanson/3513769 to your computer and use it in GitHub Desktop.
Blocking queue for connections
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
private BlockingQueue<Connection> pool = new LinkedBlockingQueue<Connection>(10); | |
private AtomicInteger connCount = new AtomicInteger(); | |
public Connection getConnection() { | |
Connection conn = pool.poll(10, TimeUnit.MILLISECONDS); | |
if (conn == null) { | |
synchronized (connCount) { | |
if (connCount.get() < 10) { | |
conn = getNewConnection(); | |
pool.offer(conn); | |
connCount.incrementAndGet(); | |
} | |
} | |
conn = pool.take(); | |
} | |
return conn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment