net.sf.jabb.util.thread
Class QueueProcessor<E>

java.lang.Object
  extended by net.sf.jabb.util.thread.QueueConsumer<E>
      extended by net.sf.jabb.util.thread.QueueProcessor<E>
Type Parameters:
E - Type of the data in the queue.
队列中数据的类型
All Implemented Interfaces:
Runnable

public abstract class QueueProcessor<E>
extends QueueConsumer<E>

A template for processing data one by one from a queue.
一个从队列中逐个取得数据进行处理的模板,数据一个一个被取走并处理。

One working thread will be created for each instance of this class when necessary.

本类的每个实例相应的会有一个工作线程在需要的时候被创建。

Author:
Zhengmao HU (James)

Field Summary
 
Fields inherited from class net.sf.jabb.util.thread.QueueConsumer
defaultThreadPool, mode, MODE_INIT, MODE_RUNNING, MODE_START, MODE_STOP_ASAP, MODE_STOP_WHEN_EMPTY, MODE_STOPPED, name, queue, thread, threadPool
 
Constructor Summary
QueueProcessor()
          Constructor to create an instance with default name and using default thread pool.
创建一个实例,使用缺省的名称和缺省的线程池。
QueueProcessor(BlockingQueue<E> workQueue)
          Constructor to create an instance with default name and using default thread pool.
创建一个实例,使用缺省的名称和缺省的线程池。
QueueProcessor(BlockingQueue<E> workQueue, String name)
          Constructor to create an instance using default thread pool.
创建一个使用缺省线程池的实例。
QueueProcessor(BlockingQueue<E> workQueue, String name, ExecutorService executorService)
          Constructor to create an instance.
创建一个实例。
QueueProcessor(String name)
          Constructor to create an instance using default thread pool.
创建一个使用缺省线程池的实例。
QueueProcessor(String name, ExecutorService executorService)
          Constructor to create an instance.
创建一个实例。
 
Method Summary
protected  void consume()
          This method is overridden over parent class so that one piece of data is taken from the queue and process(Object) is invoked.
这个方法被重载了,从而队列中的一份数据会被取出并调用process(Object)方法。
abstract  void process(E obj)
          Process one piece of data - this method should be overridden in subclass.
处理一份数据——这个方法应该在子类中被重载。
 
Methods inherited from class net.sf.jabb.util.thread.QueueConsumer
getName, getQueue, preStop, queue, run, setExecutorService, setName, setQueue, start, stop, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueueProcessor

public QueueProcessor(BlockingQueue<E> workQueue,
                      String name,
                      ExecutorService executorService)
Constructor to create an instance.
创建一个实例。

Parameters:
name - Name of this instance, which determines the naming of working thread.
本个实例的名称,会被用在工作线程名里。
workQueue - The queue that data for processing will be fetched from.
本实例将从这个队列取得待处理数据。
executorService - Thread pool that working thread will be get from.
指定让本实例从这里获得工作线程。

QueueProcessor

public QueueProcessor(String name,
                      ExecutorService executorService)
Constructor to create an instance.
创建一个实例。

Parameters:
name - Name of this instance, which determines the naming of working thread.
本个实例的名称,会被用在工作线程名里。
executorService - Thread pool that working thread will be get from.
指定让本实例从这里获得工作线程。

QueueProcessor

public QueueProcessor(BlockingQueue<E> workQueue,
                      String name)
Constructor to create an instance using default thread pool.
创建一个使用缺省线程池的实例。

Parameters:
name - Name of this instance, which determines the naming of working thread.
本个实例的名称,会被用在工作线程名里。
workQueue - The queue that data for processing will be fetched from.
本实例将从这个队列取得待处理数据。

QueueProcessor

public QueueProcessor(String name)
Constructor to create an instance using default thread pool.
创建一个使用缺省线程池的实例。

Parameters:
name - Name of this instance, which determines the naming of working thread.
本个实例的名称,会被用在工作线程名里。

QueueProcessor

public QueueProcessor(BlockingQueue<E> workQueue)
Constructor to create an instance with default name and using default thread pool.
创建一个实例,使用缺省的名称和缺省的线程池。

Parameters:
workQueue - The queue that data for processing will be fetched from.
本实例将从这个队列取得待处理数据。

QueueProcessor

public QueueProcessor()
Constructor to create an instance with default name and using default thread pool.
创建一个实例,使用缺省的名称和缺省的线程池。

Method Detail

consume

protected void consume()
This method is overridden over parent class so that one piece of data is taken from the queue and process(Object) is invoked.
这个方法被重载了,从而队列中的一份数据会被取出并调用process(Object)方法。

Specified by:
consume in class QueueConsumer<E>

process

public abstract void process(E obj)
Process one piece of data - this method should be overridden in subclass.
处理一份数据——这个方法应该在子类中被重载。

This method may be interrupted while running, so please note the following:
这个方法在运行过程中可能会遇到线程的interrupt,所以如果有以下情况要注意正确处理:

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

Parameters:
obj - The data taken from queue, which needs to be processed
从队列中取出的待处理数据。


Copyright © 2012. All Rights Reserved.