com.sun.grizzly
Class DefaultPipeline

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.AbstractSequentialList<E>
              extended by java.util.LinkedList<Callable>
                  extended by com.sun.grizzly.DefaultPipeline
All Implemented Interfaces:
Pipeline<Callable>, Serializable, Cloneable, Iterable<Callable>, Collection<Callable>, List<Callable>, Queue<Callable>
Direct Known Subclasses:
LinkedListPipeline

public class DefaultPipeline
extends LinkedList<Callable>
implements Pipeline<Callable>

Simple Thread Pool based on the wait/notify/synchronized mechanism.

Author:
Jean-Francois Arcand
See Also:
Serialized Form

Field Summary
protected  int initialByteBufferSize
          The initial ByteBuffer size for newly created WorkerThread instances
protected  boolean isStarted
          Has the pipeline already started
protected  int maxQueueSizeInBytes
          Maximum pending connection before refusing requests.
protected  int maxThreads
          The maximum number of Thread
protected  int minSpareThreads
          The minimum numbers of spare WorkerThreadImpl
protected  int minThreads
          The minimum numbers of WorkerThreadImpl
protected  String name
          The name of this Pipeline
protected  int port
          The port used.
protected  int priority
          The Thread Priority
protected  int threadCount
          The number of WorkerThreadImpl
protected  int threadsIncrement
          The increment number used when adding new thread.
protected  int waitingThreads
          The number of thread waiting for a Task
protected  WorkerThreadImpl[] workerThreads
          WorkerThreadImpl amanged by this pipeline.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
DefaultPipeline()
           
DefaultPipeline(int maxThreads, int minThreads, String name, int port)
           
DefaultPipeline(int maxThreads, int minThreads, String name, int port, int priority)
           
 
Method Summary
 void execute(Callable callable)
          Add an object to this pipeline
 boolean expireKey(SelectionKey key)
          Invoked when the SelectorThread is about to expire a SelectionKey.
 ByteBufferFactory.ByteBufferType getByteBufferType()
          The ByteBufferType used to create the ByteBuffer associated with WorkerThreadImpls created by this instance.
 int getCurrentThreadCount()
          Return current thread count
 int getCurrentThreadsBusy()
          Return the curent number of threads that are currently processing a task.
 int getInitialByteBufferSize()
          Get the initial WorkerThreadImpl ByteBuffer size
 int getMaxSpareThreads()
          Return the maximum spare thread.
 int getMaxThreads()
          Return the number of threads used by this pipeline.
 int getMinSpareThreads()
          Return the minimum spare thread.
 String getName()
          Return the name of this Pipeline
 int getQueueSizeInBytes()
          Get the maximum pending connections this Pipeline can handle.
 int getTaskQueuedCount()
          The number of Task currently queued
 int getWaitingThread()
          Return the number of waiting threads.
protected  void increaseWorkerThread(int increment, boolean startThread)
          Create new WorkerThreadImpl.
 void initPipeline()
          Init the Pipeline by initializing the required WorkerThreadImpl.
 boolean interruptThread(long threadID)
          Interrupt the Thread using it thread id
 boolean isEmpty()
          Return true if the size of this ArrayList minus the current waiting threads is lower than zero.
 void setByteBufferType(ByteBufferFactory.ByteBufferType byteBufferType)
          Set the ByteBufferType to use when creating the ByteBuffer associated with WorkerThreadImpls created by this instance.
 void setInitialByteBufferSize(int size)
          Set the initial WorkerThreadImpl ByteBuffer size
 void setMaxThreads(int maxThreads)
          Set the number of threads used by this pipeline.
 void setMinSpareThreads(int minSpareThreads)
          Set the minimum spare thread this Pipeline can handle.
 void setMinThreads(int minThreads)
          Set the minimum thread this Pipeline will creates when initializing.
 void setName(String name)
          Set the name of this Pipeline
 void setPort(int port)
          Set the port used by this Pipeline
 void setPriority(int priority)
          Set the thread priority of the Pipeline
 void setQueueSizeInBytes(int maxQueueSizeInBytesCount)
          Set the maximum pending connection this Pipeline can handle.
 void setThreadsIncrement(int threadsIncrement)
          Set the number the Pipeline will use when increasing the thread pool
 void startPipeline()
          Start the Pipeline and all associated WorkerThreadImpl
 void stopPipeline()
          Stop the Pipeline and all associated WorkerThreadImpl
 String toString()
           
 Callable waitForIoTask()
          Return a Callable object available in the pipeline.
 
Methods inherited from class java.util.LinkedList
add, add, addAll, addAll, addFirst, addLast, clear, clone, contains, element, get, getFirst, getLast, indexOf, lastIndexOf, listIterator, offer, peek, poll, remove, remove, remove, removeFirst, removeLast, set, size, toArray, toArray
 
Methods inherited from class java.util.AbstractSequentialList
iterator
 
Methods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.sun.grizzly.Pipeline
size
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, removeAll, retainAll, subList
 

Field Detail

waitingThreads

protected int waitingThreads
The number of thread waiting for a Task


maxThreads

protected int maxThreads
The maximum number of Thread


minThreads

protected int minThreads
The minimum numbers of WorkerThreadImpl


minSpareThreads

protected int minSpareThreads
The minimum numbers of spare WorkerThreadImpl


port

protected int port
The port used.


threadCount

protected int threadCount
The number of WorkerThreadImpl


name

protected String name
The name of this Pipeline


priority

protected int priority
The Thread Priority


isStarted

protected boolean isStarted
Has the pipeline already started


workerThreads

protected transient WorkerThreadImpl[] workerThreads
WorkerThreadImpl amanged by this pipeline.


maxQueueSizeInBytes

protected int maxQueueSizeInBytes
Maximum pending connection before refusing requests.


threadsIncrement

protected int threadsIncrement
The increment number used when adding new thread.


initialByteBufferSize

protected int initialByteBufferSize
The initial ByteBuffer size for newly created WorkerThread instances

Constructor Detail

DefaultPipeline

public DefaultPipeline()

DefaultPipeline

public DefaultPipeline(int maxThreads,
                       int minThreads,
                       String name,
                       int port,
                       int priority)

DefaultPipeline

public DefaultPipeline(int maxThreads,
                       int minThreads,
                       String name,
                       int port)
Method Detail

initPipeline

public void initPipeline()
Init the Pipeline by initializing the required WorkerThreadImpl. Default value is 10

Specified by:
initPipeline in interface Pipeline<Callable>

startPipeline

public void startPipeline()
Start the Pipeline and all associated WorkerThreadImpl

Specified by:
startPipeline in interface Pipeline<Callable>

stopPipeline

public void stopPipeline()
Stop the Pipeline and all associated WorkerThreadImpl

Specified by:
stopPipeline in interface Pipeline<Callable>

increaseWorkerThread

protected void increaseWorkerThread(int increment,
                                    boolean startThread)
Create new WorkerThreadImpl. This method must be invoked from a synchronized block.

Parameters:
increment - - how many additional WorkerThreadImpl objects to add
startThread - - should newly added WorkerThreadImpl objects be started after creation?

interruptThread

public boolean interruptThread(long threadID)
Interrupt the Thread using it thread id

Parameters:
threadID - - id of Thread to interrupt
Returns:
boolean, was Thread interrupted successfully ?

execute

public void execute(Callable callable)
             throws PipelineFullException
Add an object to this pipeline

Specified by:
execute in interface Pipeline<Callable>
Parameters:
callable - a Callable to add to this Pipeline
Throws:
PipelineFullException - if Pipeline is full

waitForIoTask

public Callable waitForIoTask()
Return a Callable object available in the pipeline. All Threads will synchronize on that method

Specified by:
waitForIoTask in interface Pipeline<Callable>
Returns:
Callable

expireKey

public boolean expireKey(SelectionKey key)
Invoked when the SelectorThread is about to expire a SelectionKey.

Specified by:
expireKey in interface Pipeline<Callable>
Parameters:
key - - A SelectionKey to expire
Returns:
true if the SelectorThread should expire the SelectionKey, false if not.

isEmpty

public boolean isEmpty()
Return true if the size of this ArrayList minus the current waiting threads is lower than zero.

Specified by:
isEmpty in interface Collection<Callable>
Specified by:
isEmpty in interface List<Callable>
Overrides:
isEmpty in class AbstractCollection<Callable>

getWaitingThread

public int getWaitingThread()
Return the number of waiting threads.

Specified by:
getWaitingThread in interface Pipeline<Callable>
Returns:
number of waiting threads

setMaxThreads

public void setMaxThreads(int maxThreads)
Set the number of threads used by this pipeline.

Specified by:
setMaxThreads in interface Pipeline<Callable>
Parameters:
maxThreads - maximum number of threads to use

getMaxThreads

public int getMaxThreads()
Return the number of threads used by this pipeline.

Specified by:
getMaxThreads in interface Pipeline<Callable>
Returns:
maximum number of threads

getCurrentThreadCount

public int getCurrentThreadCount()
Return current thread count

Specified by:
getCurrentThreadCount in interface Pipeline<Callable>
Returns:
current thread count

getCurrentThreadsBusy

public int getCurrentThreadsBusy()
Return the curent number of threads that are currently processing a task.

Specified by:
getCurrentThreadsBusy in interface Pipeline<Callable>
Returns:
current busy thread count

getMaxSpareThreads

public int getMaxSpareThreads()
Return the maximum spare thread.

Returns:
maximum spare thread count

getMinSpareThreads

public int getMinSpareThreads()
Return the minimum spare thread.

Returns:
minimum spare thread count

setMinSpareThreads

public void setMinSpareThreads(int minSpareThreads)
Set the minimum spare thread this Pipeline can handle.

Parameters:
minSpareThreads - minimum number of spare threads to handle

setPriority

public void setPriority(int priority)
Set the thread priority of the Pipeline

Specified by:
setPriority in interface Pipeline<Callable>
Parameters:
priority - thread priority to use

setName

public void setName(String name)
Set the name of this Pipeline

Specified by:
setName in interface Pipeline<Callable>
Parameters:
name - Pipeline name to use

getName

public String getName()
Return the name of this Pipeline

Specified by:
getName in interface Pipeline<Callable>
Returns:
the name of this Pipeline

setPort

public void setPort(int port)
Set the port used by this Pipeline

Specified by:
setPort in interface Pipeline<Callable>
Parameters:
port - the port used by this Pipeline

setMinThreads

public void setMinThreads(int minThreads)
Set the minimum thread this Pipeline will creates when initializing.

Specified by:
setMinThreads in interface Pipeline<Callable>
Parameters:
minThreads - the minimum number of threads.

toString

public String toString()
Overrides:
toString in class AbstractCollection<Callable>

setThreadsIncrement

public void setThreadsIncrement(int threadsIncrement)
Set the number the Pipeline will use when increasing the thread pool

Specified by:
setThreadsIncrement in interface Pipeline<Callable>
Parameters:
threadsIncrement - amount to increase thread pool by

getTaskQueuedCount

public int getTaskQueuedCount()
The number of Task currently queued

Returns:
number of queued connections

setQueueSizeInBytes

public void setQueueSizeInBytes(int maxQueueSizeInBytesCount)
Set the maximum pending connection this Pipeline can handle.

Specified by:
setQueueSizeInBytes in interface Pipeline<Callable>
Parameters:
maxQueueSizeInBytesCount - maximum queue size (in bytes) this Pipeline should use

getQueueSizeInBytes

public int getQueueSizeInBytes()
Get the maximum pending connections this Pipeline can handle.

Returns:
maximum queue size (in bytes) this Pipeline is using

getInitialByteBufferSize

public int getInitialByteBufferSize()
Get the initial WorkerThreadImpl ByteBuffer size

Returns:
initial WorkerThreadImpl ByteBuffwaitingThreadser size

setInitialByteBufferSize

public void setInitialByteBufferSize(int size)
Set the initial WorkerThreadImpl ByteBuffer size

Parameters:
size - initial WorkerThreadImpl ByteBuffer size

getByteBufferType

public ByteBufferFactory.ByteBufferType getByteBufferType()
The ByteBufferType used to create the ByteBuffer associated with WorkerThreadImpls created by this instance.

Returns:
The ByteBufferType used to create the ByteBuffer associated with WorkerThreadImpls created by this instance.

setByteBufferType

public void setByteBufferType(ByteBufferFactory.ByteBufferType byteBufferType)
Set the ByteBufferType to use when creating the ByteBuffer associated with WorkerThreadImpls created by this instance.

Parameters:
byteBufferType - The ByteBuffer type.


Copyright © 2010 SUN Microsystems. All Rights Reserved.