标签:
接口CallBack
public interface CallBack {
/**回调方法*/
public void execute(Object ... objects);
}
调用方Local
public class Local implements CallBack ,Runnable{
private Remote remote;//被调用方作为调用方的一个引用
private String msg;
public Local(Remote remote, String msg) {
super();
this.remote = remote;
this.msg = msg;
}
@Override
public void execute(Object... objects) {
//返回的消息
System.out.println(objects[0]);
System.out.println(Thread.currentThread().getName());
Thread.interrupted();
}
public void sendMessage() {
System.out.println(Thread.currentThread().getName());
Thread thread = new Thread(this);
thread.start();
System.out.println("发送消息");
}
public static void main(String[] args)
{
Local local = new Local(new Remote(),"Hello");
local.sendMessage();
}
@Override
public void run() {
remote.executeMessage(msg, this);
}
}
被调用方Remote
public class Remote {
public void executeMessage(String msg,CallBack callBack){
System.out.println(msg);
System.out.println("接收到了Local传过来的命令");
callBack.execute(new String[]{"Hello Local"});
}
}
标签:
原文地址:http://www.cnblogs.com/anxue/p/4724628.html