JAVA异步加回调的例子
package com.sunchao.callback;
/**
* callback interface
* @author Administrator
*
*/
public interface CallBack {
/**
* execute the callback method
* @param objects make the asyn execute result as the parameter of callback method
*/
public void execute(Object... objects );
}
package com.sunchao.callback;
/**
* Local class which use to send the message
* to the remote class
* @author Administrator
*
*/
public class Local implements CallBack,Runnable {
private Remote remote;
private String message; public Local(String message, Remote remote){
super();
this.remote = remote;
this.message = message;
}
@Override
public void run() {
this.remote.executeMessage(message, this);
} /**
* this method is used by the handler class
* to callback
*/
@Override
public void execute(Object... objects) {
/**
* print the result of handler class
* and send to the local
*/
System.out.println(objects[0]);
System.out.println(Thread.currentThread().getName());
}
/**
* new a thread to handle the message;
*/
public void sendMessage(){
System.out.println(Thread.currentThread().getName());
Thread newThread = new Thread(this);
newThread.start();
System.out.println("The message has been send!");
} public static void main(String args[]){
Local local = new Local("hello", new Remote());
local.sendMessage();
}
}
package com.sunchao.callback;
/**
* the remote class which used by to handle
* the message which send from the local class
* @author Administrator
*
*/
public class Remote {
/**
* the method used to handle the message
* @param msg the message send from the callback class
* @param callBack the callback class
*/
public void executeMessage(String msg, CallBack callBack){
/**
* the empty loop represent the remote class is busying to
* handler the message
*/
for(int i = 0; i < 10000; i++){ }
System.out.println("oh my god ,i have done the message from the local : " + msg); /**
* the remote handler has done the message,and now
* to notify the local class
*/
callBack.execute((Object[])new String[]{"nice to see again!"});
} }
JAVA异步加回调的例子的更多相关文章
- jquery zTree异步加载的例子
下面是使用zTree异步加载的一个例子: 1)初始化树的时候是ajax请求,返回nodes列表来初始化树的:如果一开始就异步的话,$.fn.zTree.init($("#zTree" ...
- android技巧(五)一个异步+接口回调的例子
public class DataBaseUtils { // 当前数据库地址 private String DB_PATH; // 备份后数据库保存地址 private String DB_BACK ...
- ajax异步加载小例子....(通俗易懂)
web.html(用户端): <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- js同步、异步、回调的执行顺序以及闭包的理解
首先,记住同步第一.异步第二.回调最末的口诀 公式表达:同步=>异步=>回调 看一道经典的面试题: for (var i = 0; i < 5; i++) { setTimeout( ...
- ANDROID_MARS学习笔记_S04_009_用java.lang.ref.SoftReference作缓存,android.os.Handler和new Thread异步加载略图片
一.简介 二.代码流程 1.private Map<String, SoftReference<Drawable>> imageCache = new HashMap<S ...
- android官方开源的高性能异步加载网络图片的Gridview例子
这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...
- wemall app商城源码中基于JAVA的Android异步加载图片管理器代码
wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改.本文分享其中 ...
- Java 爬虫遇上数据异步加载,试试这两种办法!
这是 Java 爬虫系列博文的第三篇,在上一篇 Java 爬虫遇到需要登录的网站,该怎么办? 中,我们简单的讲解了爬虫时遇到登录问题的解决办法,在这篇文章中我们一起来聊一聊爬虫时遇到数据异步加载的问题 ...
- js回调与异步加载的用法
以前还是菜鸟的时候(虽然现在依然很菜 -_-|| )对异步加载与回调函数的技术无比向往,但也一直没有使用过,这次因为页面逻辑太过复杂,一堆请求逻辑,如果还是用顺序请求,页面的速度... 领导又要挠头了 ...
随机推荐
- Python的伪私有属性
什么是伪私有属性? 在Python中,没有类似 private 之类的关键字来声明私有方法或属性. Python中要声明私有属性,需要在属性前加上双下划线(但是结尾处不能有双下划线),如:self._ ...
- #多个关联的python程序在linux后台运行
由于在shell脚本中直接使用&符号和python程序有冲突,不能正常的进入后台执行python(多番尝试python xxx.py &,后面的&总是不能正常识别.系统cent ...
- <The Art of Readable Code> 笔记二 (下)
第1章 封装信息到名字 (Packing information into names) 4 附加额外信息 1) encode value type 对于某些变量,附加额外的信息可以让人更好的理 ...
- VirtualBoX虚拟机里安装linux系统,在虚拟系统里安装增强功能报错解决方法
http://www.cnblogs.com/MoShin/archive/2012/04/25/2469156.html 当我们在虚拟机里安装lixunx系统,避免不了的要安装增强功能,无论是视觉效 ...
- 【干货】分享几个写 demo 的思路
好久没有动笔,最近发现了一个新的写 demo 的思路,仔细一想,自己仿佛积累了不少写 demo 的思路和想法,总结一下,抛砖引玉. 本文所说 demo 主要分以下三种: 本地 demo 外链 demo ...
- 使用jemeter手工编写注册、登陆脚本 运用 fiddler (三)
手工注册脚本的录制 我们可以发现 编写手工脚本 需要用到两个软件一起合作才能编写成功 工作亦是如此 养成良好的团队合作精神 是必不可少的 现在我们来看手工编写注册脚本 第一步 :我们先进入网站首页 ...
- TLD网络资源汇总--学习理解之(四)
原文:http://blog.csdn.net/mysniper11/article/details/8726649 引文地址:http://www.cnblogs.com/lxy2017/p/392 ...
- Zabbix实战-简易教程(7)--监控第一台host
一.安装 agent 1.1 Agent分布 1.2 Agent安装 基础模板安装方法: wget -qO- http://zbxinstall.168.com:18888/base/agent-in ...
- mysql 在B数据库下 创建一个与A数据库中一样的表
1.创建数据内容与结构一致(不会复制索引以及外键) create table B.test as select * from A.test; 2.把上面的步骤分开,先复制结构 create table ...
- vs2012 .net4.0 nuget 导入NHibernate4.0版本
问题描述: 最近弄一个项目,打算使用NHibernate,本人使用的VS2012,项目用的是.NET 4.0.在使用Nuget安装引用的时候,发现安装失败,提示如下图: 意思是当前安装的NHibern ...