Android Java Handler,线程同步块(notifyall& Wait)

时间:2014-10-04 13:00:55

标签: java android handler wait

你好,这不是我的主要代码,但它的关闭应该表明我的问题。

我需要创建UI延迟(两次) - 一个接一个 我无法单独使用handler.postDelay。

所以我尝试用线程制作它。

我想做的是让t2在t1结束后才开始。

任何人都可以帮助我吗?

   final Handler handler1 = new Handler();
     final Handler handler2 = new Handler();


     synchronized(this)
     {


     Thread t1= new Thread(new Runnable() {

        public void run()
        {

            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Do something after 3s = 3000ms
                  imgB1.setBackgroundColor(Color.RED);
              notifyAll();
                }
            }, 3000);

        }
     });



     Thread t2= new Thread(new Runnable() {

        public void run()
        {

              handler2.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      // Do something after 3s = 3000ms
                      imgB1.setBackgroundColor(Color.YELLOW);
                      }
              }, 3000);

        }
       });



     t1.start();



     while (t1.isAlive()  )
     {
        try {
            wait();

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }


     t2.start();

     }

2 个答案:

答案 0 :(得分:2)

保持简单:

  • 一个处理程序,没有线程
  • 发布延迟的runnable做两件事:
    1. 设置红色背景
    2. 发布另一个延迟的runnable(设置黄色背景)

final Handler handler1 = new Handler(Looper.getMainLooper());
handler1.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 3s = 3000ms
              imgB1.setBackgroundColor(Color.RED);
              //post another action to be executed in 3 sec
              handler1.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      // Do something after 3s = 3000ms
                      imgB1.setBackgroundColor(Color.YELLOW);
                  }
              }, 3000);
            }
        }, 3000);

答案 1 :(得分:1)

您也可以通过这种方式保持简单:

  • 一个线程,没有处理程序

  Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(3000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                imgB1.setBackgroundColor(Color.RED);    
                            }
                        });
                        SystemClock.sleep(3000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                imgB1.setBackgroundColor(Color.YELLOW); 
                            }
                        });
                    }
                });
                t.start();
相关问题