queueprocessorthread.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 93 行

JAVA
93
字号
/*
 * $Id: QueueProcessorThread.java,v 1.1 2003/11/25 11:41:47 epr Exp $
 */
package org.jnode.util;

import org.jnode.system.BootLog;

/**
 * @author epr
 */
public class QueueProcessorThread extends Thread  {
	
	/** The queue i'm processing */
	private final Queue queue;
	/** The actual processor */
	private final QueueProcessor processor;
	private boolean stop;

	/**
	 * Create a new instance
	 * @param name
	 * @param queue
	 * @param processor
	 */
	public QueueProcessorThread(String name, Queue queue, QueueProcessor processor) {
		super(name);
		this.queue = queue;
		this.processor = processor;
		this.stop = false;
	}
	
	/**
	 * Create a new instance. A new queue is automatically created.
	 * @param name
	 * @param processor
	 * @see #getQueue()
	 */
	public QueueProcessorThread(String name, QueueProcessor processor) {
		this(name, new Queue(), processor);
	}
	
	/**
	 * Stop the processor
	 */
	public void stopProcessor() {
		this.stop = true;
		//this.interrupt();
	}
	
	/**
	 * Handle an exception thrown during the processing of the object.
	 * @param ex
	 */
	protected void handleException(Exception ex) {
		BootLog.error("Exception in QueueProcessor", ex);
	}
	
	/**
	 * Handle an exception thrown during the processing of the object.
	 * @param ex
	 */
	protected void handleError(Error ex) {
		BootLog.error("Error in QueueProcessor", ex);
	}
	
	/**
	 * Thread runner
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		while (!stop) {
			try {
				final Object object = queue.get(false);
				if (object != null) {
					processor.process(object);
				}
			} catch (Exception ex) {
				handleException(ex);
			} catch (Error ex) {
				handleError(ex);
			}
		}
	}

	/**
	 * Gets this queue this thread works on.
	 * @return The queue
	 */
	public Queue getQueue() {
		return queue;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?