android中運用CountDownLatch


以下場景,一個thread等待UI線程更新狀態后才可以使用

public class CountDown extends Activity{
    private CountDownLatch mCountDownLatch;
    private static final int GET_TEST_MSG=100;
    private boolean mTestFlag=false;

    final Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_TEST_MSG:
                    mTestFlag=true;
                    mCountDownLatch.countDown();
                    break;
            }

        }
    };

    private void testThread(){
        mTestFlag=false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                mCountDownLatch=new CountDownLatch(1);
                mHandler.sendMessage(mHandler.obtainMessage(GET_TEST_MSG));

                try{
                    mCountDownLatch.await();

                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM