Tomcat的最大连接数通过LimitLatch来控制
protected LimitLatch initializeConnectionLatch() {
if (maxConnections==-1) return null;
if (connectionLimitLatch==null) {
connectionLimitLatch = new LimitLatch(getMaxConnections());
}
return connectionLimitLatch;
}
LimitLatch有一个内部类Sync继承了AbstractQueuedSynchronizer 这个是juc的基础
private class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = 1L;
public Sync() {
}
@Override
protected int tryAcquireShared(int ignored) {
long newCount = count.incrementAndGet();
if (!released && newCount > limit) {
// Limit exceeded
count.decrementAndGet();
return -1;
} else {
return 1;
}
}
@Override
protected boolean tryReleaseShared(int arg) {
count.decrementAndGet();
return true;
}
}
相当于实现了一个共享锁,其中count是当前的连接数,limit是所配置的最大连接数,如果当前连接数大于所配置最大连接数放回-1,则阻塞,否则返回1,获取锁