Object Pool
Example - Connection pool
class ConnectionPool {
private Set<Connection> available = new HashSet<>();
private Set<Connection> taken = new HashSet<>();
public ConnectionPool() {
available.add(new Connection());
available.add(new Connection());
available.add(new Connection());
}
public Connection getConnection() {
Connection connection = available.iterator().next();
available.remove(connection);
taken.add(connection);
return connection;
}
public void returnConnection(Connection connection) {
taken.remove(connection);
available.add(connection);
}
public String toString() {
return "Available: " + available.size() + ", taken: " + taken.size();
}
}
class Connection {
}Last updated