在項目中須要一個線程在一個線程池後全部線程執行完畢後進行調用,因爲線程池的數量不定,因此CountDownLatch、CyclicBarrier和Semaphore都用不上,就只能從wait何notify入手。java
單個線程:ide
public void onDealedDataPkgId(long l) { if (l < 0) { synchronized (mEndObject) { while (!mEndObject.isTranslateEnd) { try { mEndObject.wait(); } catch (InterruptedException e) { e.printStackTrace(); } mEndObject.isTranslateEnd = true; mHandler.post(new Runnable() { @Override public void run() { mOutListener.onTranslateEnd(); } }); } } } }
線程池:post
OnlineApi.postTranslator(fromLan, toLan, realText, new CommonCallBack() { @Override public void onSuccess(Call call, String result) { ... if (end) { synchronized (mEndObject) { mEndObject.notifyAll(); } ... } } catch (Exception e) { e.printStackTrace(); } }