JAVA CallBack详解 之 异步回调

参考
https://www.cnblogs.com/dong-liu/p/7476955.html

上一篇文章讲述了同步回调,本篇文章讲讲异步回调,用线程实现。

回调机制是什么哪?先上一幅图来说明一下吧,这里用老师问问题,学生回答问题为例子,解释一下回调机制的使用
在这里插入图片描述
首先需要一个接口callback,以及一个继承了接口的类Teacher。Teahcer类中有另一个类Student的对象,在Teacher中执行函数,会调用student中的方法,student执行对应方法后再回调Teacher中重写的接口方法,这样就完成了一次回调方法。

1. 首先定义一个回调的接口

package callBackTest;

public interface Callback {
    public void tellAnswer(int answer);
    
}

2. 接下来定义老师的类,实现这个接口(老师类内部需要包含一个学生的对象)

package callBackTest;

public class Teacher implements Callback {
    private Student student;
    
    public Teacher(Student student){
        this.student = student;
    }
    
    public void askQuestion(final String question){
        System.out.println("Teacher ask a question: "+ question);    //提出一个问题
        student.resolveQuestion(this,question);                      //询问学生
        System.out.println("Teacher: do someting else");             //忙自己的事
    } 
    
    @Override              //重写回调函数
    public void tellAnswer(int answer){
        System.out.println("your answer is: " + answer);
    }

}

3. 最后定义学生的类

package callBackTest;

public class Student {
    
    public void resolveQuestion(Callback callback, final String question){
        
        System.out.println("Student receive the question: " + question);    //学生收到老师的问题
        System.out.println("Student: I am busy");                             
        doSomething();                                       //学生在忙其他的事
        callback.tellAnswer(2);                    //忙完之后回答问题
    }
    
    public void doSomething(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

4. 最后我们来测试一下回调机制

package callBackTest;

public class Test {
    public static void main(String[] args){
        Student st = new Student();
        Teacher th = new Teacher(st);
        th.askQuestion("1+1=?");
    }

}

我们看看运行后的结果

Teacher ask a question: 1+1=?
Student receive the question: 1+1=?
Student: I am busy
your answer is: 2
Teacher: do someting else

从运行结果可以看出,这里的回调机制其实是同步的,也就是说老师问过问题之后,会阻塞在那里,等待学生回答问题,然后才会去做自己的事情。

但实际中,老师问完问题,并不会等待学生回答,而是继续做其他的事情,也就是异步回调。

实现异步回调,我们只需要修改Teacher这个类就可以了,将调用学生回答问题的操作封装在一个线程中就可以了,也就是黄色部分。

package callBackTest;

public class Teacher implements Callback {
    private Student student;
    
    public Teacher(Student student){
        this.student = student;
    }
    
    public void askQuestion(final String question){
        System.out.println("Teacher ask a question: "+ question);
        new Thread(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                student.resolveQuestion(Teacher.this,question);              //这里需要注意必须声明是Teacher.this

            }
            
        }).start();
        System.out.println("Teacher: do someting else");
    }
    
    @Override
    public void tellAnswer(int answer){
        System.out.println("your answer is: " + answer);
    }

}

我们再来看看运行后的结果,这里可以看出老师问过问题后就去忙自己的事情了,等学生回答完问题之后,就回调了自己的函数tellAnswer

Teacher ask a question: 1+1=?
Teacher: do someting else
Student receive the question: 1+1=?
Student: I am busy
your answer is: 2
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值