参考来源
同步调用:类B的对象B调用类A的对象方法,需要等待A的方法执行完后才执行B下面程序
class A {public void methodA() {System.out.println("this is class A method");}} class B {public void methodB() {A a = new A();a.methodA();System.out.println("this is class B method");}}public class Main {public static void main(String[] args) {B b = new B();b.methodB();//打印A再打印B}}
异步调用:对象B调用继承线程A类对象方法,无须等待A类执行完即可执行下面的,常用于多线程
class A extends Thread {@Overridepublic void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("this is class A method");}}class B {public void methodB() {A a = new A();a.start();System.out.println("this is class B method");}}public class Main {public static void main(String[] args) {B b = new B();b.methodB();//虽然A先执行,但由于等待3s导致先输出B,再输出A}}
回调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口。
对象a的方法methodA()中调用对象b的methodB()方法,在对象b的methodB()方法中反过来调用对象a的callBack()方法,这个callBack()方法称为回调函数,这种调用方法称为回调
class A {public void methodA() {B b = new B();b.methodB(new A());System.out.println("this is class A method : methodA");}public void callBack() {System.out.println("this is class A method : callBack");}} class B {public void methodB(A a) {System.out.println("this is class B method : methodB");a.callBack();}} public class Main {public static void main(String[] args) {A a = new A();a.methodA();//methodB->callBack->methodA}}