wait和notify工做运用记录

在项目中须要一个线程在一个线程池后全部线程执行完毕后进行调用,因为线程池的数量不定,因此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();
                    }
                }
相关文章
相关标签/搜索