EventPullSource源码分享

 

  
  
package org.eredlab.g4.rif.pushlet.core;
import org.eredlab.g4.rif.pushlet.util.Log;
/**
 * Abstract Event source from which Events are pulled.
 *
 * @author
 * @since 2011-05-12
 */
abstract public class EventPullSource implements EventSource, Runnable {
        private volatile boolean alive = false;
        private volatile boolean active = false;
        private static int threadNum = 0;
        private Thread thread;
        public EventPullSource() {
        }
        abstract protected long getSleepTime();
        abstract protected Event pullEvent();
        public void start() {
                thread = new Thread(this, "EventPullSource-" + (++threadNum));
                thread.setDaemon(true);
                thread.start();
        }
        public boolean isAlive() {
                return alive;
        }
        /**
         * Stop the event generator thread.
         */
        public void stop() {
                alive = false;
                if (thread != null) {
                        thread.interrupt();
                        thread = null;
                }
        }
        /**
         * Activate the event generator thread.
         */
        synchronized public void activate() {
                if (active) {
                        return;
                }
                active = true;
                if (!alive) {
                        start();
                        return;
                }
                Log.debug(getClass().getName() + ": notifying...");
                notifyAll();
        }
        /**
         * Deactivate the event generator thread.
         */
        public void passivate() {
                if (!active) {
                        return;
                }
                active = false;
        }
        /**
         * Main loop: sleep, generate event and publish.
         */
        public void run() {
                Log.debug(getClass().getName() + ": starting...");
                alive = true;
                while (alive) {
                        try {
                                Thread.sleep(getSleepTime());
                                // Stopped during sleep: end loop.
                                if (!alive) {
                                        break;
                                }
                                // If passivated wait until we get
                                // get notify()-ied. If there are no subscribers
                                // it wasts CPU to remain producing events...
                                synchronized (this) {
                                        while (!active) {
                                                Log.debug(getClass().getName() + ": waiting...");
                                                wait();
                                        }
                                }
                        } catch (InterruptedException e) {
                                break;
                        }
                        try {
                                // Derived class should produce an event.
                                Event event = pullEvent();
                                // Let the publisher push it to subscribers.
                                Dispatcher.getInstance().multicast(event);
                        } catch (Throwable t) {
                                Log.warn("EventPullSource exception while multicasting ", t);
                                t.printStackTrace();
                        }
                }
                Log.debug(getClass().getName() + ": stopped");
        }
}
 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值