孝感市门户网站管理中心,石河子网站设计,内蒙网络_网站建设,wordpress安装后空白页软件模块之间总是存在着一定的接口#xff0c;从调用方式上#xff0c;可以把他们分为三类#xff1a;同步调用、回调和异步调用。同步调用是一种阻塞式调用#xff0c;调用方要等待对方执行完毕才返回#xff0c;它是一种单向调用#xff1b;回调是一种双向调用模式从调用方式上可以把他们分为三类同步调用、回调和异步调用。同步调用是一种阻塞式调用调用方要等待对方执行完毕才返回它是一种单向调用回调是一种双向调用模式也就是说被调用方在接口被调用时也会调用对方的接口异步调用是一种类似消息或事件的机制不过它的调用方向刚好相反接口的服务在收到某种讯息或发生某种事件时会主动通知客户方(即调用客户方的接口)。回调和异步调用的关系非常紧密通常我们使用回调来实现异步消息的注册通过异步调用来实现消息的通知。同步调用是三者当中最简单的而回调又常常是异步调用的基础回调你可以这样来理解A发送消息给BB处理好A要求的事情后将结果返回给AA再对B返回的结果来做进一步的处理。A、回调的实现/*** 回调接口* author KOOK**/public interface CallBack {/*** 执行回调方法* param objects将处理后的结果作为参数返回给回调方法*/public void execute(Object... objects );}B、消息的发送者/*** 简单本地发送异步消息的类* author KOOK**/public class Local implements CallBack,Runnable{/*** 远程接收消息的类模拟point-to-point*/private Remote remote;/*** 发送出去的消息*/private String message;public Local(Remote remote, String message) {super();this.remote remote;this.message message;}/*** 发送消息*/public void sendMessage(){/**当前线程的名称**/System.out.println(Thread.currentThread().getName());/**创建一个新的线程发送消息**/Thread thread new Thread(this);thread.start();/**当前线程继续执行**/System.out.println(Message has been sent by Local~!);}/*** 发送消息后的回调函数*/public void execute(Object... objects ) {/**打印返回的消息**/System.out.println(objects[0]);/**打印发送消息的线程名称**/System.out.println(Thread.currentThread().getName());/**中断发送消息的线程**/Thread.interrupted();}public static void main(String[] args){Local local new Local(new Remote(),Hello);local.sendMessage();}public void run() {remote.executeMessage(message, this);}}C、远程消息的接收者/*** 这个类相当于你的同学*/public class Remote {/*** 处理消息* param msg 接收的消息* param callBack 回调函数处理类*/public void executeMessage(String msg,CallBack callBack){/**模拟远程类正在处理其他事情可能需要花费许多时间**/for(int i0;i1000000000;i){}/**处理完其他事情现在来处理消息**/System.out.println(msg);System.out.println(I hava executed the message by Local);/**执行回调**/callBack.execute(new String[]{Nice to meet you~!}); //这相当于同学执行完之后打电话给你}}回调的好处之一:摒弃了继承抽象类方式的回调方式更加简便灵活public abstract class B{public void execute(){getConnection();doCRUD();releaseConnection();}public abstract void doCRUD();public void getConnection(){System.out.println(获得连接...);}public void releaseConnection(){System.out.println(释放连接...);}}public class A extends B{public void doCRUD(){System.out.println(执行add操作...);}public void add(){doCRUD();}}public class C extends B{public void doCRUD(){System.out.println(执行delete操作...);}public void delete(){doCRUD();}}如果改为回调实现是这样的interface CallBack{public void doCRUD();}public class HibernateTemplate {public void execute(CallBack action){getConnection();action.doCRUD();releaseConnection();}public void add(){execute(new CallBack(){public void doCRUD(){System.out.println(执行add操作...);}});}public void delete(){execute(new CallBack(){public void doCRUD(){System.out.println(执行delete操作...);}});}public void getConnection(){System.out.println(获得连接...);}public void releaseConnection(){System.out.println(释放连接...);}}