假如如今有一个Buttonbutton,Buttonbutton上有click和doubleclick事件。

两个不同的事件须要进行不同的处理。这时候就须要为对应的事件注冊Listener了。改动后的文件夹组织结构例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWF6aGltYXpo/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

1、事件基本类的编写例如以下:

package com.event.test02;

public class Event {

	public String action; // 事件名称
public String message; // 附加说明 /**
* @param action
* @param message
*/
public Event(String action, String message) {
this.action = action;
this.message = message;
}
}
package com.event.test02;

public class EventNames {
/**
* 单击事件
*/
public static final String Click = "Click"; /**
* 双击事件
*/
public static final String DoubleClick = "DoubleClick";
}
package com.event.test02;

import com.event.test02.exception.EventAlreadyRegisteredException;
import com.event.test02.handler.ClickHandler;
import com.event.test02.handler.DoubleClickHandler; public class EventBinder {
public static void bindEvents(){
try {
EventDispatcher eventDispatcher = EventDispatcher.getInstance();
eventDispatcher.addEventListener(EventNames.Click, new ClickHandler());
eventDispatcher.addEventListener(EventNames.DoubleClick, new DoubleClickHandler());
} catch (EventAlreadyRegisteredException e) {
e.printStackTrace();
}
} }
package com.event.test02;

import java.util.HashMap;

import com.event.test02.exception.EventAlreadyRegisteredException;
import com.event.test02.exception.UnknowEventException;
import com.event.test02.handler.EventListener; public class EventDispatcher { private static EventDispatcher eventDispatcher; private EventDispatcher(){} /**
* 获取实例
* @return EventDispatcher
*/
public static EventDispatcher getInstance(){
if(eventDispatcher == null){
eventDispatcher = new EventDispatcher();
}
return eventDispatcher;
} HashMap<String, EventListener> map = new HashMap<String, EventListener>(); /**
* 加入监听器
* @param event
* @param listener
* @throws EventAlreadyRegisteredException
*/
public void addEventListener(String event, EventListener listener)
throws EventAlreadyRegisteredException {
if(map.get(event) != null){
throw new EventAlreadyRegisteredException();
}
map.put(event, listener);
} /**
* 移除某一个监听器
* @param event
*/
public void removeEventListener(String event) {
map.remove(event);
} /**
* @param e
* @throws UnknowEventException
*/
public void dispatchEvent(Event e) throws UnknowEventException {
EventListener listener = map.get(e.action);
if(listener == null){
throw new UnknowEventException();
}else{
listener.handleEvent(e);
}
} /**
* 移除全部监听器
*/
public void removeAllListeners() {
map.clear();
} }

2、监听器类的编写例如以下

package com.event.test02.handler;

import com.event.test02.Event;

public interface EventListener {
public void handleEvent(Event e);
}
package com.event.test02.handler;

import com.event.test02.Event;

public class DoubleClickHandler implements EventListener{

	public void handleEvent(Event e) {
System.out.println("hand doubleclick event ....");
}
}
package com.event.test02.handler;

import com.event.test02.Event;

public class ClickHandler implements EventListener{

	public void handleEvent(Event e) {
System.out.println("hand click event ....");
}
}

3、自己定义异常处理类

package com.event.test02.exception;

public class EventAlreadyRegisteredException extends Exception {

	private static final long serialVersionUID = 741821449383395827L;

}
package com.event.test02.exception;

public class UnknowEventException extends Exception {
private static final long serialVersionUID = 4829929946904208467L;
}

最后执行的结果例如以下:

hand  click event ....
hand  doubleclick event ....

如上的程序灵活了不少,其组织也很清晰。

在使用的时候不要拘泥于某一种形式。

在实际项目开发的过程中,能够将很多大的操作封装为一个专门的任务Task,也能够启动若干个承担不同任务的线程。仅仅要给这些任务定义一个事件名称,当须要执行某个任务时,利用事件触发就可以。

有兴趣的读者能够去看一个框架disruptor,推荐的地址例如以下:

http://ifeve.com/disruptor/

Java异步编程第2篇的更多相关文章

  1. java异步编程降低延迟

    目录 java异步编程降低延迟 一.ExecutorService和CompletionService 二.CompletableFuture(重要) 三.stream中的parallel(并行流) ...

  2. Paip.Php Java 异步编程。推模型与拉模型。响应式(Reactive)”编程FutureData总结... 1

    Paip.Php  Java 异步编程.推模型与拉模型.响应式(Reactive)"编程FutureData总结... 1.1.1       异步调用的实现以及角色(:调用者 提货单) F ...

  3. 【Todo】【读书笔记】Java多线程编程指南-设计模式篇

    下了这本书<Java多线程编程指南-设计模式篇>, 还有另一本<JAVA多线程设计模式>,据说内容有重复,结合着看.

  4. Java 异步编程 (5 种异步实现方式详解)

    ​ 同步操作如果遇到一个耗时的方法,需要阻塞等待,那么我们有没有办法解决呢?让它异步执行,下面我会详解异步及实现@mikechen 目录 什么是异步? 一.线程异步 二.Future异步 三.Comp ...

  5. Java 异步编程的几种方式

    前言 异步编程是让程序并发运行的一种手段.它允许多个事情同时发生,当程序调用需要长时间运行的方法时,它不会阻塞当前的执行流程,程序可以继续运行,当方法执行完成时通知给主线程根据需要获取其执行结果或者失 ...

  6. Java异步编程——深入源码分析FutureTask

    Java的异步编程是一项非常常用的多线程技术. 之前通过源码详细分析了ThreadPoolExecutor<你真的懂ThreadPoolExecutor线程池技术吗?看了源码你会有全新的认识&g ...

  7. Java 异步编程

    昨天头儿给的学习文档我还没看完,头儿说:“MongoDB光会简单的添删改查什么的不行,要深入了解,你们连$set和$inc使用场景都分不清.” 确实,学习过一年多SQL,确实对学习MongoDB有点影 ...

  8. java并发编程系列原理篇--JDK中的通信工具类Semaphore

    前言 java多线程之间进行通信时,JDK主要提供了以下几种通信工具类.主要有Semaphore.CountDownLatch.CyclicBarrier.exchanger.Phaser这几个通讯类 ...

  9. java并发编程JUC第九篇:CountDownLatch线程同步

    在之前的文章中已经为大家介绍了java并发编程的工具:BlockingQueue接口.ArrayBlockingQueue.DelayQueue.LinkedBlockingQueue.Priorit ...

随机推荐

  1. IM即时通讯群组头像拼接.net core 解决方案

    一.需求概述 多人聊天(群组,讨论组,聊天室,以下统称: “群组” )生成一个拼接头像,需要把最先加入群组的几个人(最多4个人,以下简称:头部用户,A.B.C.D)的头像拼凑成在一起. 群组创建后,A ...

  2. 跳转移动端js代码

    <script language="JavaScript"> $(function(){ var MobileUA = (function() { var ua = n ...

  3. Context都没弄明白,还怎么做Android开发?

    Activity mActivity =new Activity() 作为Android开发者,不知道你有没有思考过这个问题,Activity可以new吗?Android的应用程序开发采用JAVA语言 ...

  4. stretchableImageWithLeftCapWidth气泡拉伸

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...

  5. Windows下批处理命令启动项目bat脚本

    文件env.cfg #server name SERVER_NAME=ActivitiService #JDK Home JDK_HOME= #Main MAIN_CLASS=com.nbtv.com ...

  6. 360Webscan Bypass

    来到select正则: ? 1 \<.+javascript:window\[.{1}\\x|<.*=(&#\d+?;?)+?>|<.*(data|src)=data: ...

  7. linux 系统时间硬件时间同步

    1.设置系统时间:date-s 21/05/2016 date -s 08:21:21 2.系统时间同步到硬件时间: clock --systohc

  8. layui.layer独立组件--解释

    网站文档-链接:http://www.layui.com/doc/modules/layer.html layer至今仍作为layui的代表作,她的受众广泛并非偶然,而是这五年多的坚持,不断完善和维护 ...

  9. Spring学习七----------Bean的配置之自动装配

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...

  10. Xenomai 3 migration

    Xenomai 3 的rtdm驱动更像一般的Linux驱动,named device会在/dev/rtdm/xxx创建一个设备文件.而用户空间使用时,写得来也和Linux的一般char设备相似,ope ...