A simple callback implementation

@FunctionalInterface
public interface CallBack {
	public void report(int num);
}

public class Boss implements CallBack {

	public void order(Employee e) {
		System.out.println("boss 下达任务,等待员工去完成。。。");
		Thread t = new Thread() {
			public void run() {
				e.complete((i) -> {
					report(i);
				});
			}
		};
		t.start();
		bossDoOtherWork();
	}

	private void bossDoOtherWork() {
		System.out.println("boss do other work");
	}

	@Override
	public void report(int num) {
		System.out.println("员工报告回馈信息" + num);
	}

	public static void main(String[] args) {
		new Boss().order(new Employee());
	}

}

public class Employee {

	public void complete(CallBack callback) {
		System.out.println("Work begin。。。");
		try {
			
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("I have completed the work");
		callback.report(3);
	}
}

Guess you like

Origin blog.csdn.net/sdkdeveloper/article/details/104633323