假如如今有一个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. win8.1安装VMware Error:This product may not be installed on a comuputer that has Microsoft HyperV installed

    之前用的win7,安装虚机没遇到这问题,换了win8.1后,再安装虚机,就会出现下面的错误.没办法,还是记录一下吧. Error:This product may not be installed o ...

  2. ios为app应用添加icon

    在工程中打开plist文件,添加,选择icon files,然后添加不同分辨率的icon名称即可.如果clean后再运行程序还是没有看到效果,那么就删除掉app包然后 再次运行就可以看到效果了.

  3. 执行sudo命令时的提示语,如何修改?

    如图所示,执行sudo命令,提示语(有中文和英文两个版本): 上面的提示内容是sudo软件原生的内容. 使用下面的方法,有的时候是可行的.sudo -p '提示语' 命令 如果要修改sudo软件原生的 ...

  4. js CacheQueue

    (function(){ var CacheQueue=function(name,weightValue,maxLength,clearTimerTime){ //public this.name ...

  5. python 制作wordcloud词云

    pip install wordcloud 需要用到numpy  pillow matplotlib 安装完成以后 wordcloud_cli --text in.txt --imagefile ou ...

  6. CentOS SVN 服务器搭建

    源码目录:/home/user/project 工程名:project 工程目录:/source/svn/project 访问地址:svn://ip/project 一. 安装svn yum inst ...

  7. 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])

    遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...

  8. 谈 API 的撰写 - 子系统

    在做一个系统时,有一些子系统几乎是必备的:配置管理,CLI,以及测试框架. 配置管理 我们先说配置管理.一个系统的灵活度,和它的配置管理是离不开的.系统中存在的大量的预置的属性(下文简称 proper ...

  9. Ubuntu下安装JDK图文解析

    我们在64位的Ubuntu中安装JDK,选择的是jdk1.6.0_32版本号.安装文件名称为jdk-6u32-linux-x64.bin(这个是64位系统的),假设是32位系统的还须要去官网下载32位 ...

  10. Shell脚本之:字符串

    字符串可以用单引号,也可以用双引号,也可以不用引号. 单引号 str='this is a string' 单引号字符串的限制: 1.单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 2 ...