Skip to content

Commit

Permalink
Minor: fixed OResourcePool.getMaxResources()
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Jul 31, 2015
1 parent be26632 commit 2bcab1d
Showing 1 changed file with 10 additions and 7 deletions.
Expand Up @@ -19,10 +19,6 @@
*/
package com.orientechnologies.common.concur.resource;

import com.orientechnologies.common.concur.lock.OInterruptedException;
import com.orientechnologies.common.concur.lock.OLockException;
import com.orientechnologies.common.log.OLogManager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -32,6 +28,10 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import com.orientechnologies.common.concur.lock.OInterruptedException;
import com.orientechnologies.common.concur.lock.OLockException;
import com.orientechnologies.common.log.OLogManager;

/**
* Generic non reentrant implementation about pool of resources. It pre-allocates a semaphore of maxResources. Resources are lazily
* created by invoking the listener.
Expand All @@ -47,10 +47,12 @@ public class OResourcePool<K, V> {
protected final Queue<V> resources = new ConcurrentLinkedQueue<V>();
protected final Queue<V> resourcesOut = new ConcurrentLinkedQueue<V>();
protected final Collection<V> unmodifiableresources;
private final int maxResources;
protected OResourcePoolListener<K, V> listener;
protected volatile int created = 0;

public OResourcePool(final int maxResources, final OResourcePoolListener<K, V> listener) {
public OResourcePool(final int iMaxResources, final OResourcePoolListener<K, V> listener) {
maxResources = iMaxResources;
if (maxResources < 1)
throw new IllegalArgumentException("iMaxResource must be major than 0");

Expand All @@ -63,7 +65,8 @@ public V getResource(K key, final long maxWaitMillis, Object... additionalArgs)
// First, get permission to take or create a resource
try {
if (!sem.tryAcquire(maxWaitMillis, TimeUnit.MILLISECONDS))
throw new OLockException("No more resources available in pool. Requested resource: " + key + " timeout: " + maxWaitMillis);
throw new OLockException("No more resources available in pool (max=" + maxResources + "). Requested resource: " + key);

} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new OInterruptedException(e);
Expand Down Expand Up @@ -110,7 +113,7 @@ public V getResource(K key, final long maxWaitMillis, Object... additionalArgs)
}

public int getMaxResources() {
return sem.availablePermits();
return maxResources;
}

public int getAvailableResources() {
Expand Down

0 comments on commit 2bcab1d

Please sign in to comment.