Hard Worker
Hard Worker

Reputation: 1111

Android Java Handler, Thread synchronized block (notifyall & Wait )

Hello this is not my main code but its close enought to show my problem .

i need to create UI delay (twice) - one after the other i couldnt make it with handler.postDelay alone.

so i have tried making it with Threading .

What i want to do is let t2 start only after t1 ends.

can anyone help me out ?

   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();

     }

Upvotes: 2

Views: 2203

Answers (2)

mmlooloo
mmlooloo

Reputation: 18977

You can also keep it simple in this way:

  • one thread, no handler

  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();

Upvotes: 1

ben75
ben75

Reputation: 28706

Keep it simple :

  • one handler, no threads
  • post a delayed runnable doing 2 things :
    1. set the red background
    2. posting another delayed runnable (setting the yellow background)

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);

Upvotes: 2

Related Questions