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异步加回调的例子的更多相关文章

  1. jquery zTree异步加载的例子

    下面是使用zTree异步加载的一个例子: 1)初始化树的时候是ajax请求,返回nodes列表来初始化树的:如果一开始就异步的话,$.fn.zTree.init($("#zTree" ...

  2. android技巧(五)一个异步+接口回调的例子

    public class DataBaseUtils { // 当前数据库地址 private String DB_PATH; // 备份后数据库保存地址 private String DB_BACK ...

  3. ajax异步加载小例子....(通俗易懂)

    web.html(用户端): <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  4. js同步、异步、回调的执行顺序以及闭包的理解

    首先,记住同步第一.异步第二.回调最末的口诀 公式表达:同步=>异步=>回调 看一道经典的面试题: for (var i = 0; i < 5; i++) { setTimeout( ...

  5. ANDROID_MARS学习笔记_S04_009_用java.lang.ref.SoftReference作缓存,android.os.Handler和new Thread异步加载略图片

    一.简介 二.代码流程 1.private Map<String, SoftReference<Drawable>> imageCache = new HashMap<S ...

  6. android官方开源的高性能异步加载网络图片的Gridview例子

    这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...

  7. wemall app商城源码中基于JAVA的Android异步加载图片管理器代码

    wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改.本文分享其中 ...

  8. Java 爬虫遇上数据异步加载,试试这两种办法!

    这是 Java 爬虫系列博文的第三篇,在上一篇 Java 爬虫遇到需要登录的网站,该怎么办? 中,我们简单的讲解了爬虫时遇到登录问题的解决办法,在这篇文章中我们一起来聊一聊爬虫时遇到数据异步加载的问题 ...

  9. js回调与异步加载的用法

    以前还是菜鸟的时候(虽然现在依然很菜 -_-|| )对异步加载与回调函数的技术无比向往,但也一直没有使用过,这次因为页面逻辑太过复杂,一堆请求逻辑,如果还是用顺序请求,页面的速度... 领导又要挠头了 ...

随机推荐

  1. grep命令及基本正则表达式

    grep命令是Linux系统中一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功 ...

  2. Netty对WebSocket的支持(五)

    Netty对WebSocket的支持(五) 一.WebSocket简介 在Http1.0和Http1.1协议中,我们要实现服务端主动的发送消息到网页或者APP上,是比较困难的,尤其是现在IM(即时通信 ...

  3. MicroPython教程之TPYBoard v102 CAN总线通信

    0x00前言 CAN是控制器局域网络(ControllerAreaNetwork,CAN)的简称,是ISO国际标准化的串行通信协议.CAN总线结构简单,只需2根线与外部相连,并且内部集成了错误探测和管 ...

  4. 【Python3之字符编码】

    一.字符集和字符编码 1.定义 计算机中储存的信息都是用二进制数表示的,而我们在屏幕上看到的英文.汉字等字符是二进制数转换之后的结果.通俗的说,按照何种规则将字符存储在计算机中,如'a'用什么表示,称 ...

  5. PHP连接LDAP进行登录验证

    基于安全性考虑,准备把PHP做的自动化平台加入ldap登录验证,具体做法如下: 了解背景: LDAP 的全称是"轻量级目录访问协议(Lightweight Directory Access ...

  6. Tsung测试Tcp协议的应用或接口

    利用Tsung模拟基于Tcp的业务流程,实属无奈.因ConnectManager部署在linux下,其中,Loadrunner的winsocket因不支持linux platform而无法使用,而Jm ...

  7. spark 1.6 完全分布式平台搭建

    软件环境: scala-2.11.4.tgz spark-1.6.2-bin-hadoop2.6.tgz 操作步骤: 一.  安装scala 1. 解压scala (tar –zxvf  filena ...

  8. 带新手走进神秘的HTTP协议

    在开发的时候经常需要访问网络,比如Android就有好多这方面的框架:Volley.OkHttp.Retrofit等,当你看这些框架源码时,可能会很好奇关于http的部分,它的首部字段是什么意思,ht ...

  9. mybatis注解开发,动态sql

    在利用mybatis注解开始时,如果没有用到动态sql时,可以直接写 @Select("select * from order") List<XlSubOrder> g ...

  10. Python 集合 深浅copy

    一,集合. 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...