Class Resource

java.lang.Object
com.j_spaces.kernel.pool.Resource
All Implemented Interfaces:
IResource
Direct Known Subclasses:
BusPacket, ByteBufferRedoLogFileStorage.ByteArrayResource, CompressedMarshObjectConvertor, ConnectionResource, ConnectionResource, MarshObjectConvertor, PacketSerializer.InputStreamsResource, ReadWriteLockResource, TraceableResource

public abstract class Resource extends Object implements IResource
Resources should extend this class and implement all abstract methods. When done using the resource, it should be released.

Usage:
    MyResourceFactory factory = new MyResourceFactory();
    ResourcePool pool = new ResourcePool(factory, 10, 50);

    MyResource resource = pool.getResource();
    try {
    ...
    }finally {
      resource.release();
    }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Atomically acquires a free resource.
    abstract void
    Cleans the resource of its contents so it can be returned to the pool.
    boolean
    An indication if this resource is currently acquired.
    boolean
    An indication if this resource originated from the pool or was allocated on the fly.
    void
    Atomically sets this resource as free; usually when returning a resource to the pool.
    void
    setAcquired(boolean acquired)
    An indication if this resource is acquired.
    void
    setFromPool(boolean fromPool)
    An indication if this resource originated from the pool or was allocated on the fly.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Resource

      public Resource()
  • Method Details

    • setFromPool

      public void setFromPool(boolean fromPool)
      Description copied from interface: IResource
      An indication if this resource originated from the pool or was allocated on the fly. The latter, will not return to the pool.
      Specified by:
      setFromPool in interface IResource
      Parameters:
      fromPool - true if originated from the pool; false if not part of the pool.
    • isFromPool

      public boolean isFromPool()
      Description copied from interface: IResource
      An indication if this resource originated from the pool or was allocated on the fly. The latter, will not return to the pool.
      Specified by:
      isFromPool in interface IResource
      Returns:
      true if originated from the pool; false if allocated.
    • acquire

      public boolean acquire()
      Description copied from interface: IResource
      Atomically acquires a free resource. But, due to race conditions, it may fail.
      Specified by:
      acquire in interface IResource
      Returns:
      true if acquire succeeded; false otherwise (may have already been acquired by another thread).
    • setAcquired

      public void setAcquired(boolean acquired)
      Description copied from interface: IResource
      An indication if this resource is acquired.
      Specified by:
      setAcquired in interface IResource
      Parameters:
      acquired - true if resource is acquired. false if resource is not acquired.
    • isAcquired

      public boolean isAcquired()
      Description copied from interface: IResource
      An indication if this resource is currently acquired. Usually a resource becomes acquired after a call to IResource.acquire(), or by use of it's status indicator IResource.setAcquired(boolean).
      Specified by:
      isAcquired in interface IResource
      Returns:
      true if resource is acquired. false if not yet acquired.
    • release

      public void release()
      Description copied from interface: IResource
      Atomically sets this resource as free; usually when returning a resource to the pool.

      Before releasing the resource back into the pool, IResource.clear() is invoked.

      Specified by:
      release in interface IResource
    • clear

      public abstract void clear()
      Description copied from interface: IResource
      Cleans the resource of its contents so it can be returned to the pool.

      You may use the IResource.isFromPool() indication to decide of a special action before returning a resource to the pool, or discard any actions on resources not returning to the pool.

      Specified by:
      clear in interface IResource
      See Also: