com.meidusa.amoeba.util.LoopingThread.java Source code

Java tutorial

Introduction

Here is the source code for com.meidusa.amoeba.util.LoopingThread.java

Source

/**
 * <pre>
 *  This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *  You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * </pre>
 */
package com.meidusa.amoeba.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * ???? ????
 */
public class LoopingThread extends Thread {

    private static Log log = LogFactory.getLog(LoopingThread.class);

    protected boolean _running = true;

    public LoopingThread() {
    }

    /**
     * ??
     */
    public LoopingThread(String name) {
        super(name);
    }

    /**
     * ?????
     */
    public synchronized void shutdown() {
        _running = false;

        // only kick the thread if it's not requesting it's own shutdown
        if (this != Thread.currentThread()) {
            kick();
        }
    }

    /**
     * 
     */
    public void run() {
        if (log.isDebugEnabled()) {
            log.debug(this.getName() + " LoopingThread willStart....");
        }
        try {
            willStart();

            while (isRunning()) {
                try {
                    iterate();
                } catch (Exception e) {
                    handleIterateFailure(e);
                }
            }
        } finally {
            didShutdown();
        }
    }

    /**
     * ???falseiterate  {@link #run}
     */
    public synchronized boolean isRunning() {
        return _running;
    }

    /**
     * ???
     */
    protected void kick() {
        // nothing doing by default
    }

    /**
     * ????
     */
    protected void willStart() {
    }

    protected void iterate() {
        throw new RuntimeException("Derived class must implement iterate().");
    }

    protected void handleIterateFailure(Exception e) {
        // log the exception

        // and shut the thread down
        log.error("error:", e);
        shutdown();
    }

    /**
     * ?shutdown???
     */
    protected void didShutdown() {
    }

}