package com.HuiDiao;
/**
*接口中包含回调函数,具体实现方法
*
*/
public interface CallBack {
public void excute(Object ...object);
//public void otherMethod();
}
package com.HuiDiao;
/**
* Master包含Excuting引用,对它发出命令。
* Excuting类包含CallBack类的引用,执行完命令后回调
* 输出结果
*/
public class Master implements CallBack {
/**
* @param args
*/
private Excuting ex=null;
public Master(Excuting e){
ex=e;
}
public void command(){
System.out.println("excute Start");
//异步调用
new Thread(new Runnable() {
@Override
public void run() {
try {
ex.excute(Master.this, new String("do work"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
System.out.println("main thread continue");
}
@Override
public void excute(Object... object) {
// TODO Auto-generated method stub
System.out.println(object[0]);
}
}
package com.HuiDiao;
import java.util.concurrent.TimeUnit;
public class Excuting {
/**
*
*/
public void excute(CallBack master,Object object) throws InterruptedException{
//if(object[0]==)
TimeUnit.SECONDS.sleep(10);//模拟任务需要的时间
master.excute("work done");//回调显示结果
}
}
package com.HuiDiao;
public class test {
/**
* 测试
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Excuting ex=new Excuting();
Master master=new Master(ex);
master.command();
}
}