Skip to content

Instantly share code, notes, and snippets.

@petejohanson
Created August 29, 2012 14:51
Show Gist options
  • Save petejohanson/3513769 to your computer and use it in GitHub Desktop.
Save petejohanson/3513769 to your computer and use it in GitHub Desktop.
Blocking queue for connections
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