【第21期】观点:人工智能到底用 GPU?还是用 FPGA?

Tomcat 8.x 《设计模式》Lifecycle & State 模式

原创 2016年08月31日 00:44:01
package cn.java.note.state;
public interface Lifecycle {

	public static final String BEFORE_INIT_EVENT = "before_init";
	public static final String AFTER_INIT_EVENT = "after_init";
	public static final String START_EVENT = "start";
	public static final String BEFORE_START_EVENT = "before_start";
	public static final String AFTER_START_EVENT = "after_start";
	public static final String STOP_EVENT = "stop";
	public static final String BEFORE_STOP_EVENT = "before_stop";
	public static final String AFTER_STOP_EVENT = "after_stop";
	public static final String AFTER_DESTROY_EVENT = "after_destroy";
	public static final String BEFORE_DESTROY_EVENT = "before_destroy";
	public static final String PERIODIC_EVENT = "periodic";
	public static final String CONFIGURE_START_EVENT = "configure_start";
	public static final String CONFIGURE_STOP_EVENT = "configure_stop";

	public void addLifecycleListener(LifecycleListener listener);
	public LifecycleListener[] findLifecycleListeners();
	public void removeLifecycleListener(LifecycleListener listener);
	public void init() throws LifecycleException;
	public void start() throws LifecycleException;
	public void stop() throws LifecycleException;
	public void destroy() throws LifecycleException;
	public LifecycleState getState();
	public String getStateName();
	public interface SingleUse {
	}
}

package cn.java.note.state;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class LifecycleBase implements Lifecycle {
	private volatile LifecycleState state = LifecycleState.NEW;
	private final List<LifecycleListener> lifecycleListeners = new CopyOnWriteArrayList<>();

	/**
	 * 设置状态
	 */
	protected synchronized void setState(LifecycleState state)
			throws LifecycleException {
		setStateInternal(state, null, true);
	}

	/**
	 * 设置状态
	 */
	protected synchronized void setState(LifecycleState state, Object data) throws LifecycleException {
		setStateInternal(state, data, true);
	}

	/**
	 * 设置状态
	 */
	private synchronized void setStateInternal(LifecycleState state, Object data, boolean check) throws LifecycleException {
		this.state = state;
		String lifecycleEvent = state.getLifecycleEvent(); // 事件名称,如 “before_init” “start”
		if (lifecycleEvent != null) {
			fireLifecycleEvent(lifecycleEvent, data); // 触发“监听器”
		}
	}

	/**
	 * 触发事件监听器
	 * @param type
	 * @param data
	 */
	protected void fireLifecycleEvent(String type, Object data) {
		LifecycleEvent event = new LifecycleEvent(this, type, data);
		for (LifecycleListener listener : lifecycleListeners) {
			listener.lifecycleEvent(event); // 触发事件
		}
	}

	/**
	 * 添加事件监听器
	 */
	@Override
	public void addLifecycleListener(LifecycleListener listener) {
		lifecycleListeners.add(listener);
	}

	/**
	 * 查看事件监听器
	 */
	@Override
	public LifecycleListener[] findLifecycleListeners() {
		return lifecycleListeners.toArray(new LifecycleListener[0]);
	}

	/**
	 * 删除事件监听器
	 */
	@Override
	public void removeLifecycleListener(LifecycleListener listener) {
		lifecycleListeners.remove(listener);
	}

	/**
	 * 初始化
	 */
	@Override
	public final synchronized void init() throws LifecycleException {
		if (!state.equals(LifecycleState.NEW)) {
			// invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
		}
		setStateInternal(LifecycleState.INITIALIZING, null, false); // 触发事件

		// initInternal(); //!!!!

		setStateInternal(LifecycleState.INITIALIZED, null, false); // 触发事件
	}

	/**
	 * 启动
	 */
	@Override
	public final synchronized void start() throws LifecycleException {

		if (LifecycleState.STARTING_PREP.equals(state) || LifecycleState.STARTING.equals(state) ||
				LifecycleState.STARTED.equals(state)) {
			return;
		}

		if (state.equals(LifecycleState.NEW)) {
			init();//!!!调用初始化
		} else if (state.equals(LifecycleState.FAILED)) {
			stop();
		} else if (!state.equals(LifecycleState.INITIALIZED) &&
				!state.equals(LifecycleState.STOPPED)) {
			// invalidTransition(Lifecycle.BEFORE_START_EVENT);
		}

		setStateInternal(LifecycleState.STARTING_PREP, null, false); // 触发事件

		// startInternal(); //触发事件

		if (state.equals(LifecycleState.FAILED)) {
			stop();
		} else if (!state.equals(LifecycleState.STARTING)) {
			// invalidTransition(Lifecycle.AFTER_START_EVENT);
		} else {
			setStateInternal(LifecycleState.STARTED, null, false);// 触发事件
		}
	}

	/**
	 * 停止
	 */
	@Override
	public final synchronized void stop() throws LifecycleException {
		if (LifecycleState.STOPPING_PREP.equals(state) || LifecycleState.STOPPING.equals(state) ||
				LifecycleState.STOPPED.equals(state)) {
			return;
		}

		if (state.equals(LifecycleState.NEW)) {
			state = LifecycleState.STOPPED;
			return;
		}

		if (!state.equals(LifecycleState.STARTED) && !state.equals(LifecycleState.FAILED)) {
			// invalidTransition(Lifecycle.BEFORE_STOP_EVENT);
		}

		if (state.equals(LifecycleState.FAILED)) {
			fireLifecycleEvent(BEFORE_STOP_EVENT, null);
		} else {
			setStateInternal(LifecycleState.STOPPING_PREP, null, false);
		}

		// stopInternal();

		if (!state.equals(LifecycleState.STOPPING) && !state.equals(LifecycleState.FAILED)) {
			// invalidTransition(Lifecycle.AFTER_STOP_EVENT);
		}

		setStateInternal(LifecycleState.STOPPED, null, false);
	}

	/**
	 * 销毁
	 */
	@Override
	public final synchronized void destroy() throws LifecycleException {
		if (LifecycleState.FAILED.equals(state)) {
			stop();
		}

		if (LifecycleState.DESTROYING.equals(state) ||
				LifecycleState.DESTROYED.equals(state)) {
			return;
		}
		if (!state.equals(LifecycleState.STOPPED) &&
				!state.equals(LifecycleState.FAILED) &&
				!state.equals(LifecycleState.NEW) &&
				!state.equals(LifecycleState.INITIALIZED)) {
			// invalidTransition(Lifecycle.BEFORE_DESTROY_EVENT);
		}
		setStateInternal(LifecycleState.DESTROYING, null, false);

		// destroyInternal();

		setStateInternal(LifecycleState.DESTROYED, null, false);
	}

	@Override
	public LifecycleState getState() {
		return state;
	}

	@Override
	public String getStateName() {
		return getState().toString();
	}
}


package cn.java.note.state;

import java.util.EventObject;

public final class LifecycleEvent extends EventObject {

	private static final long serialVersionUID = 1L;

	public LifecycleEvent(Lifecycle lifecycle, String type, Object data) {
		super(lifecycle);
		this.type = type;
		this.data = data;
	}

	private final Object data;

	private final String type;


	public Object getData() {
		return data;
	}


	public Lifecycle getLifecycle() {
		return (Lifecycle) getSource();
	}

	public String getType() {
		return this.type;
	}
}

package cn.java.note.state;
public final class LifecycleException extends Exception {

    private static final long serialVersionUID = 1L;

    public LifecycleException() {
        super();
    }

    public LifecycleException(String message) {
        super(message);
    }

    public LifecycleException(Throwable throwable) {
        super(throwable);
    }

    public LifecycleException(String message, Throwable throwable) {
        super(message, throwable);
    }
}

package cn.java.note.state;
public interface LifecycleListener {
    public void lifecycleEvent(LifecycleEvent event);
}

package cn.java.note.state;
/**
 * 生命周期状态
 */
public enum LifecycleState {
    NEW(false, null),
    INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT),
    INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT),
    STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT),
    STARTING(true, Lifecycle.START_EVENT), // available = true,lifecycleEvent="start"
    STARTED(true, Lifecycle.AFTER_START_EVENT),// available = true,lifecycleEvent="after_start"
    STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT),// available = true,lifecycleEvent="before_stop"
    STOPPING(false, Lifecycle.STOP_EVENT),
    STOPPED(false, Lifecycle.AFTER_STOP_EVENT),
    DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT),
    DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT),
    FAILED(false, null);

    private final boolean available;
    private final String lifecycleEvent;

    private LifecycleState(boolean available, String lifecycleEvent) {
        this.available = available;
        this.lifecycleEvent = lifecycleEvent;
    }

    public boolean isAvailable() {
        return available;
    }

    public String getLifecycleEvent() {
        return lifecycleEvent;
    }
}

package cn.java.note.state;

public class MyLifecycle1 extends LifecycleBase {

	@Override
	public String toString() {
		return "MyLifecycle1 []";
	}
	
}

package cn.java.note.state;

public class MyLifecycle1Listener implements LifecycleListener {

	@Override
	public void lifecycleEvent(LifecycleEvent event) {
		MyLifecycle1 myLifecycle1 = (MyLifecycle1) event.getLifecycle();
//		System.out.println(myLifecycle1);
		if (event.getType().equals(Lifecycle.BEFORE_INIT_EVENT)) { 
			beforeInit();
		} else if (event.getType().equals(Lifecycle.AFTER_INIT_EVENT)) {
		} else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
		} else if (event.getType().equals(Lifecycle.START_EVENT)) {
		} else if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) {
		} else if (event.getType().equals(Lifecycle.BEFORE_STOP_EVENT)) {
		} else if (event.getType().equals(Lifecycle.STOP_EVENT)) {
		} else if (event.getType().equals(Lifecycle.AFTER_STOP_EVENT)) {
		} else if (event.getType().equals(Lifecycle.BEFORE_DESTROY_EVENT)) {
		} else if (event.getType().equals(Lifecycle.AFTER_DESTROY_EVENT)) {
		}
		System.out.println("trigger event : " + event.getType());
	}

	protected synchronized void beforeInit() {

	}

}

package cn.java.note.state;

public class Test {

	public static void main(String[] args) throws LifecycleException {
		MyLifecycle1 myLifecycle = new MyLifecycle1();
		myLifecycle.addLifecycleListener(new MyLifecycle1Listener());
		myLifecycle.init();
		myLifecycle.start();
		myLifecycle.stop();
		myLifecycle.destroy();
	}

}



版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Spring官方文档翻译

一、Spring框架概述 Spring框架是一个轻量级的解决方案,可以一站式地构建企业级应用。Spring是模块化的,所以可以只使用其中需要的部分。可以在任何web框架上使用控制反转(IoC),...

java高级教程

第一章 Servlet <h2 style="margin: 13pt 0cm; text-indent: 15.7pt; line-height: normal; text

Tomcat7源码解析

Tomcat7 源码阅读学习     背景     Tomcat源码目录结构     Tomcat体系结构     Tomcat源码解析           Tomcat的启动流程          ...

Jetty 的工作原理以及与 Tomcat 的比较

Jetty 的工作原理以及与 Tomcat 的比较 <a class="dwauthor" title="" href="http://www.ibm.com/developerworks/cn/java/j
  • Flory
  • Flory
  • 2012-12-10 10:08
  • 94

Tomcat8.x 《设计模式》Facade设计模式的使用->getServletContext()

Tomcat8.x Facade设计模式的使用 // 请求对象 class org.apache.catalina.connector.Request{ public Servlet...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)