javaFlacEncoder
Class BlockThreadManager

java.lang.Object
  extended by javaFlacEncoder.BlockThreadManager
All Implemented Interfaces:
java.lang.Runnable

public class BlockThreadManager
extends java.lang.Object
implements java.lang.Runnable

BlockThreadManager is used by FLACEncoder(when encoding with threads), to dispatch BlockEncodeRequests to ThreadFrames which do the actual encode. BlockThreadManager accepts BlockEncodeRequest objects to encode. For each Frame object given to encode with, a Thread is supplied. This thread will take a BlockEncodeRequest, encode it, then give it back to the BlockThreadManager The thread will then take another BlockencodeRequest, and repeat. If no block encode requests are available, it will block till available. The main thread of the BlockThreadManager will be waiting for additions to the finished queue. If the item is not the next in line to be written to file, it will be saved temporarily. If it is the next item to be written, it will be given back to the FLACEncoder to be written to output, and any saved objects will be searched to see if more can be written. The main thread will then wait for the next frame again.

Author:
Preston Lacey

Constructor Summary
BlockThreadManager(FLACEncoder encoder)
          Constructor.
 
Method Summary
 boolean addFrameThread(Frame frame)
          Add a Frame to this manager, which it will use to encode a block.
 boolean addRequest(BlockEncodeRequest ber)
          Add a BlockEncodeRequest to the manager.
 void blockWhileQueueExceeds(int count)
          This function is used to help control flow of BlockEncodeRequests into this manager.
 int getTotalManagedCount()
          Get total number of BlockEncodeRequests added to this manager, but not yet passed back to the FLACEncoder object.
 BlockEncodeRequest getWaitingRequest()
          Get a BlockEncodeRequest object which is queued for encoding, pausing for up to 0.5 seconds till one is available.
 void notifyFrameThreadExit(FrameThread ft)
          Notify this manager that a FrameThread has ended it's run() method, returning the FrameThread object to the manager for use in future Threads.
 void returnFinishedRequest(BlockEncodeRequest ber)
          Used to return a finished BlockEncodeRequest from a FrameThread.
 void run()
          Waits for the next BlockEncodeRequest that needs to be sent back to the FLACEncoder for finalizing.
 void stop()
          Notify this manager that it may stop as soon as all currently outstanding requests are completed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockThreadManager

public BlockThreadManager(FLACEncoder encoder)
Constructor. Must supply a valid FLACEncoder object which will be alerted when a block is finished encoding.

Parameters:
encoder - FLACEncoder to use in encoding process.
Method Detail

getTotalManagedCount

public int getTotalManagedCount()
Get total number of BlockEncodeRequests added to this manager, but not yet passed back to the FLACEncoder object.

Returns:
number of BlockEncodeRequests remaining in this manager.

blockWhileQueueExceeds

public void blockWhileQueueExceeds(int count)
This function is used to help control flow of BlockEncodeRequests into this manager. It will block so long as their is at least as many unprocessed blocks waiting to be encoded as the value given.

Parameters:
count - Maximum number of outstanding requests that may exist before this method may return.

addFrameThread

public boolean addFrameThread(Frame frame)
Add a Frame to this manager, which it will use to encode a block. Each Frame added allows one more thread to be used for encoding. At least one Frame must be added for this manager to encode.

Parameters:
frame - Frame to use for encoding.
Returns:
boolean false if there was an error adding the frame, true otherwise.

notifyFrameThreadExit

public void notifyFrameThreadExit(FrameThread ft)
Notify this manager that a FrameThread has ended it's run() method, returning the FrameThread object to the manager for use in future Threads.

Parameters:
ft - FrameThread object which is ending.

getWaitingRequest

public BlockEncodeRequest getWaitingRequest()
Get a BlockEncodeRequest object which is queued for encoding, pausing for up to 0.5 seconds till one is available. It is expected that this object will later(after encoding to a FLAC frame) be returned to this manager through the returnFinishedRequest method. Failure to return the finished object will cause the encoding process to hang.

Returns:
BlockEncodingRequest to encode, null if none available.

stop

public void stop()
Notify this manager that it may stop as soon as all currently outstanding requests are completed. Future calls to addRequest() will clear this stop state.


returnFinishedRequest

public void returnFinishedRequest(BlockEncodeRequest ber)
Used to return a finished BlockEncodeRequest from a FrameThread. This must only be called with a finished request, which was originally added to this manager through the addRequest() method.

Parameters:
ber - finished BlockEncodeRequest that needs passed back to the FLACEncoder object.

run

public void run()
Waits for the next BlockEncodeRequest that needs to be sent back to the FLACEncoder for finalizing. If no request is finished, or currently assigned to an encoding thread, will timeout after 0.5 seconds and end.

Specified by:
run in interface java.lang.Runnable

addRequest

public boolean addRequest(BlockEncodeRequest ber)
Add a BlockEncodeRequest to the manager. This will immediately attempt to assign a request to an encoding thread(which may not occur if no threads are currently available). Requests are passed back to the currently set FLACEncoder object when finished and ready to be written to output.

Parameters:
ber - Block request to encode
Returns:
boolean true if block added, false if an error occured.