CallBack

Java CallBack

回调函数

  • 异步处理中常用的处理方式回调。举个例子,Calling为调用者Called为被调用者以及CallBack接口
  public interface CallBack {
  	void response(String result);
  }
  public class Calling implements CallBack{
  	private Called called;
  	public void setCalled(Called called){
  		this.called = called;
  	}

  	public void askQuestion(final String question){
  		new Thread(new Runnable() {
  			@Override
  			public void run() {
  				called.execute(Calling.this, question);
  			}
  		}).start();
  		otherMethod();
  	}

  	private void otherMethod() {
  		System.out.println("Calling other mehtod!");
  	}

  	@Override
  	public void response(String result) {
  		System.out.println("called result:" + result);
  	}
  }
  public class Called {
  	public void execute(CallBack callBack, String question){
  		System.out.println("Called question: " + question);
  		try {
  			System.out.println("Called sleep 3s!");//模拟耗时
  			Thread.sleep(3000);
  		} catch (InterruptedException e) {
  			e.printStackTrace();
  		}
  		callBack.response("This is called response!");
  	}
  }
  public class CallBackTest {
  	public static void main(String[] args) {
  		Called called = new Called();
  		Calling calling = new Calling();
  		calling.setCalled(called);
  		calling.askQuestion(" who are you");
  	}
  }

  //Calling other mehtod!
  //Called question:  who are you
  //Called sleep 3s!
  //Called result:This is called response!