版权声明:本文为博主原创文章 http://blog.csdn.net/su20145104009 https://blog.csdn.net/su20145104009/article/details/80309533
在学习Java的路上很早很早就接触了回调,比如:JFrame的键盘事件,JFrame控件的点击事件,线程池执行之前的方法beforeExecute与执行之后的方法 afterExecute,线程的Run方法其原理就是使用了回调。之前一直没有研究过其原理,在这里学习一下回调模式,也方便理解事件监听模式和观察者模式。
回调模式
在计算机程序设计中,回调函数,或简称回调,是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序
可能会有这样一个需求,某开发者想在线程执行之前和执行之后分别做一些事情,你可能想到的方法就是在线程的run方法体内做这些操作,这样固然能实现,可是却使得各种业务耦合在一起。最好的解决方法就是使用回调模式。
定义一个抽象类ThreadHolder
在抽象类中有三个方法
- 线程执行的run方法
- beforeRun为线程执行之前调用的方法
- afterRun为线程执行完成之后调用的方法
abstract class ThreadHolder {
public final void run(Runnable runnable) {
Objects.requireNonNull(runnable, "runnable required not null");
beforeRun();
runnable.run();
afterRun();
}
/**
* 在线程执行之前执行
*/
protected void beforeRun() {
}
/**
* 在线程执行完之后执行
*/
protected void afterRun() {
}
}
然后实现一个简单的线程任务,继承ThreadHolder 并重写beforeRun和afterRun方法
class simpleThread extends ThreadHolder {
@Override
protected void beforeRun() {
System.out.println("执行之前。。。");
}
@Override
protected void afterRun() {
System.out.println("执行之后。。。");
}
}
测试
public class AppTest {
public static void main(String[] args) {
simpleThread simpleThread = new simpleThread();
simpleThread.run(() -> {
int n = 10;
for (int i = 0; i < n; i++) {
System.out.println("current num is :" + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
测试结果:
执行之前。。。
current num is :0
current num is :1
current num is :2
current num is :3
current num is :4
current num is :5
current num is :6
current num is :7
current num is :8
current num is :9
执行之后。。。
解释:
- ThreadHolder 抽象类run方法决定了在线程的执行前后分别调用beforeRun、afterRun方法
- simpleThread 重写beforeRun方法和afterRun方法,在方法体中做一些事情
- AppTest 中向run方法中传递线程对象
通过这个实例,我们可以看到使用回调模式实现了我们的功能。在底层代码,即ThreadHolder中我们就制定了空方法执行顺序,在高层重写空的方法,然后在执行时我们实现的方法就会被底层调用。