edu.stanford.nlp.util.concurrent
Class MulticoreWrapper<I,O>

java.lang.Object
  extended by edu.stanford.nlp.util.concurrent.MulticoreWrapper<I,O>
Type Parameters:
I - input type
O - output type

public class MulticoreWrapper<I,O>
extends java.lang.Object

Provides convenient multicore processing for threadsafe objects. Objects that can be wrapped by MulticoreWrapper must implement the ThreadsafeProcessor interface. See edu.stanford.nlp.util.concurrent.MulticoreWrapperTest and edu.stanford.nlp.tagger.maxent.documentation.MulticoreWrapperDemo for examples of use. TODO(spenceg): Handle exceptions gracefully in the queue. TODO(spenceg): This code does not support multiple consumers, i.e., multi-threaded calls to peek() and poll().

Author:
Spence Green

Constructor Summary
MulticoreWrapper(int nThreads, ThreadsafeProcessor<I,O> processor)
          Constructor.
MulticoreWrapper(int numThreads, ThreadsafeProcessor<I,O> processor, boolean orderResults)
          Constructor.
 
Method Summary
 void join()
          Blocks until all active processes finish.
 boolean peek()
          Indicates whether a not a new result is available.
 O poll()
          Returns the next available result.
 void put(I item)
          Allocate instance to a process and return.
 void setMaxBlockTime(long t)
          Maximum amount of time to block on a call to put() in milliseconds.
 java.lang.String toString()
          Return status information about the underlying threadpool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MulticoreWrapper

public MulticoreWrapper(int nThreads,
                        ThreadsafeProcessor<I,O> processor)
Constructor.

Parameters:
nThreads - If less than or equal to 0, then automatically determine the number of threads. Otherwise, the size of the underlying threadpool.
processor -

MulticoreWrapper

public MulticoreWrapper(int numThreads,
                        ThreadsafeProcessor<I,O> processor,
                        boolean orderResults)
Constructor.

Parameters:
numThreads - -- if less than or equal to 0, then automatically determine the number of threads. Otherwise, the size of the underlying threadpool.
processor -
orderResults - -- If true, return results in the order submitted. Otherwise, return results as they become available.
Method Detail

setMaxBlockTime

public void setMaxBlockTime(long t)
Maximum amount of time to block on a call to put() in milliseconds. Default 0, which indicates to never time out.

Parameters:
t -

toString

public java.lang.String toString()
Return status information about the underlying threadpool.

Overrides:
toString in class java.lang.Object

put

public void put(I item)
Allocate instance to a process and return. This call blocks until item can be assigned to a thread.

Parameters:
item - Input to a Processor

join

public void join()
Blocks until all active processes finish.


peek

public boolean peek()
Indicates whether a not a new result is available.

Returns:
true if a new result is available, false otherwise.

poll

public O poll()
Returns the next available result.

Returns:
the next completed result, or null if no result is available


Stanford NLP Group