日期:2014-05-20 浏览次数:20843 次
ListenableFuture<XX> future = ...; Futures.addCallback(future, new FutureCallback<XX> { public void onSuccess(XX result) { xx(result); } public void onFailure(Throwable t) { reportError(t); } });
------解决方案--------------------
public interface ThreadListener { public void threadStart(Object source); public void threadFinished(Object source); } public class MyThread extends Thread { public ThreadListener listener; public void setThreadListener(ThreadListener tl){ this.listener = tl; } public void run(){ if(this.listener != null) this.listener.threadStart(this); //...... if(this.listener != null) this.listener.threadFinished(this); } } public class Main implements ThreadListener { public void threadStart(Object source){ System.out.println("线程开始了"); } public void threadFinished(Object source){ System.out.println("线程结束了"); } private void test(){ Thread[] threads = new Thread[10]; for(int i=0; i<threads.length; i++){ MyThread thread = new MyThread(); thread.setThreadListener(this); thread.start(); threads[i] = thread; } for(Thread thead : threads){ thread.join(); } } public static void main(String[] args){ Test t = new Test(); t.test(); } }
------解决方案--------------------