com.dc.gameserver.extComponents.Kit.util.LoopRunnable.java Source code

Java tutorial

Introduction

Here is the source code for com.dc.gameserver.extComponents.Kit.util.LoopRunnable.java

Source

/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.extComponents.Kit.util;

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

/**
 * Runnable?,?Runnable??.??shutdown(),pause(),resume()
 * 
 * 
 * 
 * <br />
 * :
 * <pre>
 * LoopRunnable lr = new LoopRunnable(new Runnable() {
 *   public void run() {
 *      System.out.println(new Timestamp(System.currentTimeMillis()));
 *   }
 * });
 * lr.setSleepInterval(100);
 *
 * Thread t = new Thread(lr);
 * t.start();
 * </pre>
 * 
 * @author badqiu
 *
 */
public class LoopRunnable implements Runnable {
    Log log = LogFactory.getLog(LoopRunnable.class);
    Runnable delegate;
    private boolean running = false;
    private boolean paused = false;
    private long sleepInterval = 0;

    public LoopRunnable(Runnable delegate) {
        this.delegate = delegate;
    }

    public void shutdown() {
        running = false;
    }

    public void pause() {
        paused = true;
    }

    public void resume() {
        paused = false;
        synchronized (this) {
            this.notify();
        }
    }

    public boolean isRunning() {
        return running;
    }

    public boolean isPaused() {
        return paused;
    }

    public long getSleepInterval() {
        return sleepInterval;
    }

    public void setSleepInterval(long sleepTimeMillis) {
        this.sleepInterval = sleepTimeMillis;
    }

    public void run() {
        running = true;
        try {
            beforeStart();
            while (running) {
                pausedIfRequired();
                try {
                    delegate.run();
                } catch (Exception e) {
                    handleIterateFailure(e);
                }
                sleepIfRequired();
            }
        } finally {
            paused = false;
            running = false;
            afterShutdown();
        }
    }

    /**
     * ,????
     */
    protected void beforeStart() {
    }

    /**
     * ,????
     */
    protected void afterShutdown() {
    }

    protected void handleIterateFailure(Exception e) {
        if (log.isWarnEnabled()) {
            log.warn("delegate Runnable occer exception", e);
        }
    }

    private void sleepIfRequired() {
        if (sleepInterval > 0 && running && !paused) {
            try {
                Thread.sleep(sleepInterval);
            } catch (InterruptedException e) {
                //ignore
            }
        }
    }

    private void pausedIfRequired() {
        if (paused && running) {
            synchronized (this) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    //ignore
                }
            }
        }
    }

}