📄 threadpoolthread.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.resourcePool.threadPoolService;import org.butor.log.Log;;/** * Instances of this class are used as worker threads * managed by an IThreadPool instance.<br> * The IThreadPool object instantiates these objects, and * call their setTask() method to assign them a task * (a Runnable object). * Once this is done, this object run() method, calls the * run() method of the task and once this is completed, * it calls the setDone() of the IThreadPool instance. It * then waits for either the shutdown() call or a new * setTask() call. */public class ThreadPoolThread extends Thread { /** * The IThreadPool instance that manage this thread */ protected IThreadPool f_pool; /** * The task to process, may be either null if the thread * is idle or != null if the thread is currently processing * the task */ protected Runnable f_task; /** * FieldDef that is true only when shuting down the thread */ protected boolean f_shutdown;/** * This method returns true only when this thread is processing * a task * @return boolean */protected boolean isBusy() { return (null == f_task);}/** * Constructor * * @param pool com.cjc.common.resourcesMgmt.threadPool.ThreadPool The pool * that manages this thread * @param group ThreadGroup The thread group to wich this thread belongs * @param name String The name of this thread */public ThreadPoolThread(IThreadPool pool, ThreadGroup group, String name) { super(group, name); f_pool = pool; f_shutdown = false; f_task = null;}/** * This method is used to wake up the thread because a new * task is assigned to it or because it must shut itself down */protected void notifyThread() { synchronized (this) { notify(); }}/** * This is where the thread spend its time waiting and * then processing tasks */public void run() { while (!f_shutdown) { synchronized (this) { while ((null == f_task) && !f_shutdown) { try { wait(); } catch (InterruptedException e) { // Nothing to do... } } } if (null != f_task) { try { f_task.run(); } catch (Exception e) { Log.logException(this, Log.LOG_TYPE_ERROR, "run", e); } finally { f_task = null; } f_pool.doneTask(this); } }}/** * This method is called by an IThreadPool instance to assign * a new task to this object * * @param aTask java.lang.Runnable The task to run */public void setTask(Runnable aTask) { if (null != f_task) { Log.logStr(this, Log.LOG_TYPE_ERROR, "setTask", "assert(aTask == null)"); } f_task = aTask; notifyThread();}/** * This method is used to shut the thread down */public void shutdown() { f_shutdown = true; notifyThread();}/** * Returns a description of the thread * * @return java.lang.String */public String toString() { return getName() + (isBusy() ? " busy" : " idle");}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -