zoukankan      html  css  js  c++  java
  • Java callback回调

    package com.callback;
    
    public interface CSCallBack {
      public void process(String status);
    }
    package com.callback;
    
    
    public class Client implements CSCallBack {
    
      private Server server;
    
      public Client(Server server) {
          this.server = server;
      }
    
      public void sendMsg(final String msg){
          System.out.println("客户端:发送的消息为:" + msg);
          new Thread(new Runnable() {
              @Override
              public void run() {
                  server.getClientMsg(Client.this,msg);
              }
          }).start();
          System.out.println("客户端:异步发送成功");
      }
    
      @Override
      public void process(String status) {
          System.out.println("客户端:服务端回调状态为:" + status);
      }
    }
    package com.callback;
    
    public class Server {
    
      public void getClientMsg(CSCallBack csCallBack , String msg) {
          System.out.println("服务端:服务端接收到客户端发送的消息为:" + msg);
    
          // 模拟服务端需要对数据处理
          try {
              Thread.sleep(5 * 1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          System.out.println("服务端:数据处理成功,返回成功状态 200");
          String status = "200";
          csCallBack.process(status);
      }
    }
    package com.callback;
    
    public class CallBackTest {
      public static void main(String[] args) {
          Server server = new Server();
          Client client = new Client(server);
    
          client.sendMsg("Server,Hello~");
      }
    }
  • 相关阅读:
    「暑期集训day23」黑幕
    暑期集训day23考试整理
    「暑期集训day22」黑色
    暑期集训day22考试整理
    「暑期集训day21」往复
    「暑期集训day20」仰望
    日常报错
    Spring-Boot环境的快速搭建
    jsp和thymeleaf模板
    Boot的简单配置
  • 原文地址:https://www.cnblogs.com/gaojy/p/7171444.html
Copyright © 2011-2022 走看看