Java模拟异步消息的发送与回调

(转载)

 

回调你可以这样来理解:

A发送消息给BB处理好A要求的事情后,将结果返回给AA再对B返回的结果来做进一步的处理。

 

 

 

1、回调的实现

  1. /** 
  2. * 回调接口 
  3. * @author KOOK 
  4. * 
  5. */ 
  6. public interface CallBack { 
  7.     /** 
  8.      * 执行回调方法 
  9.      * @param objects   将处理后的结果作为参数返回给回调方法 
  10.      */ 
  11.     public void execute(Object... objects ); 
  12. }

 

2、 消息的发送者

  1. /** 
  2. * 简单本地发送异步消息的类 
  3. * @author KOOK 
  4. * 
  5. */ 
  6. public class Local implements CallBack,Runnable{ 
  7.      
  8.     /** 
  9.      * 远程接收消息的类,模拟point-to-point 
  10.      */ 
  11.     private Remote remote; 
  12.      
  13.     /** 
  14.      * 发送出去的消息 
  15.      */ 
  16.     private String message; 
  17.      
  18.     public Local(Remote remote, String message) { 
  19.         super(); 
  20.         this.remote = remote; 
  21.         this.message = message; 
  22.     } 
  23.  
  24.     /** 
  25.      * 发送消息 
  26.      */ 
  27.     public void sendMessage() 
  28.     { 
  29.         /**当前线程的名称**/ 
  30.         System.out.println(Thread.currentThread().getName()); 
  31.         /**创建一个新的线程发送消息**/ 
  32.         Thread thread = new Thread(this); 
  33.         thread.start(); 
  34.         /**当前线程继续执行**/ 
  35.         System.out.println("Message has been sent by Local~!"); 
  36.     } 
  37.  
  38.     /** 
  39.      * 发送消息后的回调函数 
  40.      */ 
  41.     public void execute(Object... objects ) { 
  42.         /**打印返回的消息**/ 
  43.         System.out.println(objects[0]); 
  44.         /**打印发送消息的线程名称**/ 
  45.         System.out.println(Thread.currentThread().getName()); 
  46.         /**中断发送消息的线程**/ 
  47.         Thread.interrupted(); 
  48.     } 
  49.      
  50.     public static void main(String[] args) 
  51.     { 
  52.         Local local = new Local(new Remote(),"Hello"); 
  53.          
  54.         local.sendMessage(); 
  55.     } 
  56.  
  57.     public void run() { 
  58.         remote.executeMessage(message, this); 
  59.          
  60.     } 

3 远程消息的接收者

  1. /** 
  2. * 处理消息的远程类 
  3. * @author KOOK 
  4. * 
  5. */ 
  6. public class Remote { 
  7.  
  8.     /** 
  9.      * 处理消息 
  10.      * @param msg   接收的消息 
  11.      * @param callBack  回调函数处理类 
  12.      */ 
  13.     public void executeMessage(String msg,CallBack callBack) 
  14.     { 
  15.         /**模拟远程类正在处理其他事情,可能需要花费许多时间**/ 
  16.         for(int i=0;i<1000000000;i++) 
  17.         { 
  18.              
  19.         } 
  20.         /**处理完其他事情,现在来处理消息**/ 
  21.         System.out.println(msg); 
  22.         System.out.println("I hava executed the message by Local"); 
  23.         /**执行回调**/ 
  24.         callBack.execute(new String[]{"Nice to meet you~!"}); 
  25.     } 
  26.      

 

执行Local类的main方法。

 

注意Local类中红色背景的那行:

remote.executeMessage(message, this);

executeMessage方法需要接收一个message参数,表示发送出去的消息,而CallBack参数是他自己,也就是这里的this。表示发送消息后,由Local类自己来处理,调用自身的execute方法来处理消息结果。

如果这里不是用this,而是用其他的CallBack接口的实现类的话,那就不能称之为“回调”了,在OO的世界里,那就属于“委派”。也就是说,“回调”必须是消息的发送者来处理消息结果,否则不能称之为回调。这个概念必须明确。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值