zoukankan      html  css  js  c++  java
  • 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!"});    
        }
    
    }
  • 相关阅读:
    const,var,let区别(转载)
    在windows上搭建redis集群
    Linux学习笔记—vim程序编辑器
    Linux学习笔记—文件与文件系统的压缩与打包(转载)
    Linux学习笔记—Linux磁盘与文件系统管理(转载)
    五,mysql优化——sql语句优化小技巧
    八,mysql优化——读写分离
    六,mysql优化——小知识点
    七,mysql优化——表的垂直划分和水平划分
    三,mysql优化--sql语句优化之索引一
  • 原文地址:https://www.cnblogs.com/onlysun/p/4520668.html
Copyright © 2011-2022 走看看