Improved thread safety.

This commit is contained in:
Simone Bordet 2015-10-30 11:16:20 +01:00
parent b74a89bcb9
commit 45cd1f1ce6
1 changed files with 26 additions and 10 deletions

View File

@ -72,13 +72,29 @@ public class DuplexConnectionPool implements Closeable, Dumpable, Sweeper.Sweepa
@ManagedAttribute(value = "The number of idle connections", readonly = true) @ManagedAttribute(value = "The number of idle connections", readonly = true)
public int getIdleConnectionCount() public int getIdleConnectionCount()
{ {
return idleConnections.size(); lock();
try
{
return idleConnections.size();
}
finally
{
unlock();
}
} }
@ManagedAttribute(value = "The number of active connections", readonly = true) @ManagedAttribute(value = "The number of active connections", readonly = true)
public int getActiveConnectionCount() public int getActiveConnectionCount()
{ {
return activeConnections.size(); lock();
try
{
return activeConnections.size();
}
finally
{
unlock();
}
} }
public Queue<Connection> getIdleConnections() public Queue<Connection> getIdleConnections()
@ -275,7 +291,7 @@ public class DuplexConnectionPool implements Closeable, Dumpable, Sweeper.Sweepa
unlock(); unlock();
} }
if (activeRemoved) if (activeRemoved || force)
released(connection); released(connection);
boolean removed = activeRemoved || idleRemoved || force; boolean removed = activeRemoved || idleRemoved || force;
if (removed) if (removed)
@ -374,14 +390,14 @@ public class DuplexConnectionPool implements Closeable, Dumpable, Sweeper.Sweepa
@Override @Override
public boolean sweep() public boolean sweep()
{ {
List<Sweeper.Sweepable> toSweep = new ArrayList<>(); List<Connection> toSweep = new ArrayList<>();
lock(); lock();
try try
{ {
for (Connection connection : getActiveConnections()) for (Connection connection : activeConnections)
{ {
if (connection instanceof Sweeper.Sweepable) if (connection instanceof Sweeper.Sweepable)
toSweep.add(((Sweeper.Sweepable)connection)); toSweep.add(connection);
} }
} }
finally finally
@ -389,13 +405,13 @@ public class DuplexConnectionPool implements Closeable, Dumpable, Sweeper.Sweepa
unlock(); unlock();
} }
for (Sweeper.Sweepable candidate : toSweep) for (Connection connection : toSweep)
{ {
if (candidate.sweep()) if (((Sweeper.Sweepable)connection).sweep())
{ {
boolean removed = getActiveConnections().remove(candidate); boolean removed = remove(connection, true);
LOG.warn("Connection swept: {}{}{} from active connections{}{}", LOG.warn("Connection swept: {}{}{} from active connections{}{}",
candidate, connection,
System.lineSeparator(), System.lineSeparator(),
removed ? "Removed" : "Not removed", removed ? "Removed" : "Not removed",
System.lineSeparator(), System.lineSeparator(),