我有很多线程,我从主实现。我的目的是限制对服务器的请求(服务器类中的一些方法),所以我把计数器放在关键部分之前和同步之前。我注意到,计数器只会增长并且永远不会减少......这是我的代码示例:
private static int currentRequests;
/**
* @param newStock StockState we are adding to the exist stock states.
*/
public void addStock(StockState newStock) throws OutOfBoundConcurrentException
{
if(currentRequests>MaxConcurrentRequests)
throw new OutOfBoundConcurrentException();
System.out.println(currentRequests++);
synchronized (this) {
int indx=checkExistStock(newStock.getStockName());
if(indx<0)
{
stockStates.add(newStock);
currentRequests--;
return;
}
else
{
stockStates.get(indx).updateStockValueFromStockStateClass(newStock.getStockCurrentValue());
}
}
currentRequests--;
}