JAVA 回调实例

自己写了一个例子演示JAVA中对耗时操作的回调通知,要求客户程序员实现抽象类Test中的回调接口onCompletion(),代码如下:

interface RequestListener{
	public void request(String action);
	public void onCompletion(String response);
}

abstract class Test implements RequestListener{
	@Override
	public void request(final String action) {
		System.out.println("received request-->"+action);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				onCompletion("requested work done=" + action);
			}
		}).start();
		System.out.println("request return...");
	}
	
}

public class AnyTest extends Test{
@Override
	public void onCompletion(String response) {
		System.out.println(response);
	}

	public void ownWork(){
		System.out.println("My own work...");
	}

	public static void main(String[] args){
		AnyTest at = new AnyTest();
		at.request("WORK1");
		at.ownWork();
	}
}

输出:

received request-->WORK1
request return...
My own work...
requested work done=WORK1

JAVA 回调实例,布布扣,bubuko.com

JAVA 回调实例

上一篇:GCC中的内嵌汇编语言


下一篇:JavaScript基础篇--Undefined与Null