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. SQL server Error Number

    描述 HY000 所有绑定列都是只读的. 必须是可升级的列,以使用 SQLSetPos 或 SQLBulkOperations 更改或插入行. HY000 已检测到一个旧 netlib (%s).请删 ...

  2. StringMVC POJO

    SpringMVC会按请求参数名和POJO类属性名进行自动匹配,自动为该属性填充属性值, 支持级联属性(本类包含其他类对象,如User类有一个属性为Address) 示例代码: index.jsp: ...

  3. 基于web的网上书城系统开发-----登录注册

    注册功能实现 signup.jsp //时间实现 function showLocale(objD) { var str,colorhead,colorfoot; var yy = objD.getY ...

  4. 浅析mongodb

    当爬取数据时候,我们可能需要缓存大量的数据,但是又无须任何复杂的连接操作,因此我们将选用NoSQL数据库,这种数据库比传统的关系型数据库更易于操作,这里我想主要说一下目前非常流行的MongoDB作为缓 ...

  5. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  6. 【转载】netstat--查看服务器[有效]连接数--统计端口并发数--access.log分析

    简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...

  7. ASP.NET Core 依赖注入

    一.什么是依赖注入(Denpendency Injection) 这也是个老身常谈的问题,到底依赖注入是什么? 为什么要用它? 初学者特别容易对控制反转IOC(Iversion of Control) ...

  8. hihoCoder #1053 : 居民迁移(贪心,二分搜索,google在线技术笔试模拟)

    #1053 : 居民迁移 时间限制:3000ms 单点时限:1000ms 内存限制:256MB 描述 公元2411年,人类开始在地球以外的行星建立居住点.在第1326号殖民星上,N个居住点分布在一条直 ...

  9. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  10. And Then There Was One(约瑟夫问题变形)

    题目链接:http://poj.org/problem?id=3517 And Then There Was One Time Limit: 5000MS   Memory Limit: 65536K ...