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回调与异步加载的用法
以前还是菜鸟的时候(虽然现在依然很菜 -_-|| )对异步加载与回调函数的技术无比向往,但也一直没有使用过,这次因为页面逻辑太过复杂,一堆请求逻辑,如果还是用顺序请求,页面的速度... 领导又要挠头了 ...
随机推荐
- partition length exceeds the loop-partition-table-imposed maximum of 4294967295
问题: 当大于2T的磁盘,在用parted操作的时候,会出现这样的报错,原因是因为现在的分区表是mbr,需要修改为gpt.mbr最大支持2T的空间. 解决方法: 搞清楚问题,解决方法也很简单:mkla ...
- Web服务器(Apache)与Servlet容器(Tomcat)
之前一直比较迷惑Apache与Tomcat的关系,通过查询资料,有所了解,现记录于此. Apache与Tomcat 两者定位:Apache是HTTP Web服务器,Tomcat是Web容器. 有一个非 ...
- adb 获取Android手机信息命令(2)
#Android命令 #获取手机名称 GET_PHONE_NAME = 'adb shell getprop ro.product.model' #获取手机版本 GET_PHONE_VERSION = ...
- ionic2 安装(一)
1.安装java JDK 2.安装nodejs 3.安装最新版ionic 指令:npm install ionic@latest 4.安装cordova 指令:npm install -g cordo ...
- OpenCV探索之路(四):膨胀、腐蚀、开闭运算
腐蚀和膨胀是最基本的形态学运算. 腐蚀和膨胀是针对白色部分(高亮部分)而言的. 膨胀就是对图像高亮部分进行"领域扩张",效果图拥有比原图更大的高亮区域:腐蚀是原图中的高亮区域被蚕食 ...
- NC和NO、耳机美标和欧标的区别
NO是常开(NORMAL OPEN),就是通常即未通电状态下,是断开的,通电后在电磁线圈的作用下(吸合)处于闭合状态.NC是常闭(NORMAL CLOSE),就是通常即未通电状态下,是闭合的,通电后在 ...
- Video Target Tracking Based on Online Learning—TLD单目标跟踪算法详解
视频目标跟踪问题分析 视频跟踪技术的主要目的是从复杂多变的的背景环境中准确提取相关的目标特征,准确地识别出跟踪目标,并且对目标的位置和姿态等信息精确地定位,为后续目标物体行为分析提供足 ...
- Nginx日常维护操作(3)
一.简明nginx常用命令 1. 启动 Nginx /sbin/nginx service nginx start 2. 停止 Nginx /sbin/nginx -s stop /sbi ...
- JavaScript基础知识(概念、常量和变量)
1.JavaScript概念 JavaScript是脚本语言; 编写之后,可以直接运行(缺失了编译的过程) 2.JavaScript发展 LiveScript => JavaScri ...
- 初入python 用户输入,if,(while 循环)
python 基础 编译型: 一次性将所有程序编译成二进制文件. 缺点:开发效率低,不能跨平台 优点:运行速度快. :c ,c++语言 等等.... 解释行:当程序执行时,一行一行的解释. 优点:开发 ...