Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Java处理程序,线程同步块(notifyall&;Wait)_Java_Android_Handler_Wait - Fatal编程技术网

Android Java处理程序,线程同步块(notifyall&;Wait)

Android Java处理程序,线程同步块(notifyall&;Wait),java,android,handler,wait,Java,Android,Handler,Wait,您好,这不是我的主代码,但它几乎足以显示我的问题 我需要创建UI延迟(两次)-一个接一个 我无法单独与handler.postDelay一起完成 所以我试着用线程来实现它 我想做的是让t2只在t1结束后开始 有人能帮我吗 final Handler handler1 = new Handler(); final Handler handler2 = new Handler(); synchronized(this) { Thread t1=

您好,这不是我的主代码,但它几乎足以显示我的问题

我需要创建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();

     }
保持简单:

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


您还可以通过以下方式使其保持简单:

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


不要像这样使用线程和同步。您似乎还阻塞了主UI线程
postDelay()
对于这样的用例应该可以很好地工作,而不涉及线程。我不能只使用postDelay,因为我确实需要对第一个延迟的结束进行标志检查。请不要告诉我将第二个post延迟设置得更大,这将导致更多问题。那么我如何使用标志来实现它呢?如果可运行程序彼此依赖,那么在处理第一个后发布另一个,即从可运行程序本身发布。我有点困惑,你的意思是什么?如果有帮助,我将尝试使用“嵌套PostDelay”和post。谢谢你的回答。您不完全希望postDelayed的参数按其他顺序排列吗?如果愿意,您可以切换颜色;)它似乎正在工作,我为此花费了2天时间,非常感谢。我尝试使用方法而不是imgB1.setBackgroundColor,结果出现错误AndroidRuntime(10657):java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
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);
  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();