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. R语言命令行参数

        批量画图任务中,需要在R中传入若干参数,之前对做法是在perl中每一个任务建立一个Rscript,这种方式超级不cool,在群里学习到R的@ARGV调用方式,差不多能够达到批量任务的要求: a ...

  2. Perl 中 `cmd` 和system"cmd"的区别

    在perl中,调用系统命令有两种形势,`cmd` 和system"cmd",他们主要的区别是`cmd`会获取返回结果,而system"cmd"会直接将结果输出到 ...

  3. Adb+.net 实现微信跳一跳自动化

    第一次用adb,一开始只是想试试看能不能解析出,没有看网上的现有解析方式. 需要安卓机开启usb 调试+电脑运行.打开跳一跳的界面 点击程序 [开始]按钮即可开始,别的按钮都是调试用的 主要流程是用a ...

  4. 微信小程序开发之picker选择器组件用法

    picker组件时一个从底部弹起的可滚动的选择器(嵌入页面滚动器组件picker-view查看https://mp.weixin.qq.com/debug/wxadoc/dev/component/p ...

  5. webstorm方向键与电脑快捷键冲突

    1.桌面上,单击鼠标右键,选择"图形属性" 2.单击"选项和支持" 3.在图示框框中,修改电脑上的快捷键  

  6. [UWP]如何使用Fluent Design System (下)

    4. 兼容旧版本 FDS最常见的问题之一是如何与Fall Creators Update之前的版本兼容,其实做起来也挺简单的,ColorfulBox就实现了Creators Update与Fall C ...

  7. 【Java】CAS的乐观锁实现之AtomicInteger源码分析

    1. 悲观锁与乐观锁 我们都知道,cpu是时分复用的,也就是把cpu的时间片,分配给不同的thread/process轮流执行,时间片与时间片之间,需要进行cpu切换,也就是会发生进程的切换.切换涉及 ...

  8. 解析Linux中的VFS文件系统之文件系统的注册(二)

    继上一篇文章:http://www.cnblogs.com/linhaostudy/p/7397024.html 3. 文件系统的注册 这里的文件系统是指可能会被挂载到目录树中的各个实际文件系统,所谓 ...

  9. Java反射-中级知识掌握

    PS:本文就Java反射常用的中级知识做下汇总和分析/cnxieyang@163.com/xieyang@e6yun.com

  10. border-image详解

    一.border-image的兼容性 border-image可以说是CSS3中的一员大将,将来一定会大放光彩,其应用潜力真的是非常的惊人.可惜目前支持的浏览器有限,仅Firefox3.5,chrom ...