java异步回调demo 发表于 2015-08-24 | 分类于 java , java-concurrent-program 12345678910111213141516171819202122232425262728293031323334import java.util.concurrent.*;interface ICallback { void getResult(int s);}class Calc { public void calc(int i, ICallback callback) throws Exception { FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() { @Override public Integer call() throws Exception { int s = i * i; Thread.sleep(5000); System.out.println("do in future task. " + Thread.currentThread().getName()); callback.getResult(s); return s; } }); new Thread(futureTask).start(); }}public class App { public static void main(String[] args) throws Exception { ICallback callback = new ICallback() { @Override public void getResult(int s) { System.out.println(s); } }; new Calc().calc(1000, callback); System.out.println("do in main"); }}