以下场景,一个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();
    }

}

 

相关文章:

  • 2022-03-09
  • 2022-12-23
  • 2021-06-09
  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-03-30
猜你喜欢
  • 2021-06-14
  • 2021-09-30
  • 2021-06-12
  • 2021-07-23
  • 2021-08-10
  • 2022-12-23
相关资源
相似解决方案