How to return to main thread Android

I have a simple Activity with two buttons "On" and "Off". I want start changing color of background in cycle with button "On" and stop this with button "Off". Also I need to have red color by click on "Off" button. I have wrote simple programm and everything is fine, but I can't understand one thing. Why the last color not always red? If I use code in main threads cycle

Thread.sleep(100);

or

Thread.sleep(1000);

I always have red color, but if I set

Thread.sleep(10);

I have random last color. Why??

Thank you !!

I have this code:

public class MyActivity extends Activity {

final Handler myHandler = new Handler();

private int randColor;

final Runnable updateColor = new Runnable() {

    public void run() {
        final Random random = new Random();
        randColor = Color.rgb(random.nextInt (255), random.nextInt (255), random.nextInt (255));
        mRelativeLayout.setBackgroundColor(randColor);
    }
};

private ColorChanger myThread;

class ColorChanger extends Thread {

    private volatile boolean mIsStopped = false;

    @Override
    public void run() {
        super.run();

        do
        {
            if (!Thread.interrupted()) {
                myHandler.post(updateColor);
            }
            else
            {
                return;
            }
            try{
                Thread.sleep(100);      
            }catch(InterruptedException e){
                return; 
            }
        }
        while(true);


    }

    public void stopThis() {

        this.interrupt();
    }
}

private RelativeLayout mRelativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    mRelativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);

}

public void onflagClick(View view) {

    myThread = new ColorChanger();
    myThread.start();

}


public void onflagoffClick(View view) throws InterruptedException {

    myThread.interrupt();

    if(myThread.isAlive())  
    {
        try {
            myThread.join();    
        } catch(InterruptedException e){

        }

    }
    else    
    {
        mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.redColor));
    }

    mRelativeLayout.setBackgroundColor(getResources().getColor(R.color.redColor));

}

}

Upvotes: 0

Views: 1156

Answers (3)

cyngus
cyngus

Reputation: 1763

I agree with the previous answer-ers, but propose a different solution.

First let me say that I recommend you stop using Runnables. In general posting a Runnable to a Handler is less efficient then sending a Message, although there are very rare exceptions to this rule.

Now, if we send Messages, what should we do? What we basically want to do is keep doing whatever we're doing until a condition is hit. A great way to do this is to write a Message Handler that receives a Message, does our work (setting the color), checks if we should keep going, and if so schedules a new Message in the future to do more work. Let's see how we might do this.

Assume the code below is inside an Activity.

private static final int MSG_UPDATE_COLOR = 1;
private static final int DELAY = 10; //10 millis

private final Object mLock = new Object();
private boolean mContinue = true;

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_UPDATE_COLOR:
                synchronized (mLock) {
                    if (mContinue) {
                        setColor(Color.rgb(random.nextInt (255), random.nextInt (255), random.nextInt (255)));
                        mHandler.sendEmptyMessageDelayed(MSG_UPDATE_COLOR, DELAY);
                    } else {
                        setColor(Color.RED);
                    }
                }
                break;
            }
        }
    }
}

public void onflagClick(View view) {

    mHandler.sendEmptyMessage(MSG_UPDATE_COLOR);

}

public void onflagoffClick(View view) throws InterruptedException {
    synchronized (mLock) {
        mContinue = false;
    }

    // cancel any pending update
    mHandler.removeMessages(MSG_UPDATE_COLOR);
    // schedule an immediate update
    mHandler.sendEmptyMessage(MSG_UPDATE_COLOR);
}

Okay, so, what is happening here. We've created a Handler that will do all the color updates. We kick that off when our start event happens. Then the Message schedules a new message (and therefore color update) in ten milliseconds. When the stop event happens we reset a flag that the message handler reads to determine if a new update should be scheduled. We then unschedule all update messages because it might be scheduled for several milliseconds in the future and instead send an immediate message that does the final color update.

For bonus points we eliminate the use of a second thread which saves resources. Looking carefully I've used synchronized blocks, but these are actually unnecessary because everything is happening on the main thread. I included these just in case someone was changing mContinue from a background thread. Another great point of this strategy is that all color updates happen in one place in the code so it is easier to understand.

Upvotes: 2

noni
noni

Reputation: 2947

As DeeV told you, Handler sends Runnables to a Looper that is basically a Thread looping inside processing messages or runnables in each loop. You are queuing messaged to the main Looper and then you are sleeping your worker Thread. Its possible that you are sending for example 2 runnables in a row between each loop of your worker thread, but the main looper has only executed the last one so you cannot see each color as you want.

If you want a simple solution to make it work, you can use an Object or a CountDownLatch to synchronize your main Looperwith your worker Thread.

For example: Just before you will sleep your worker Thread you can do the next thing myLockObject.wait()

Then, you should change post(Runnable) to sendMessage(Message). In handleMessage from your Handler you can do myLockObject.notify() (Keep in mind that handleMessage will be executed inside the Looper that you have created your Handler or you can specify any Looper you want explicity). To obtain a new Message you should use myHandler.obtainMessage().

This will make your worker Thread wait your main Looperto process your runnable just before you wait X time until you post next color. Obviously you should create your new Object as a field of your Activity for example:

private myLockObject = new Object()

Upvotes: 0

DeeV
DeeV

Reputation: 36045

When you post to Handler, it will run your Runnable at some given time in the future. It is not immediate. It also works in a queue so the more times you post to Handler you are going to stack up the commands that will all get executed in order eventually.

You're facing a race condition because with Thread.sleep(10), the program is most likely stacking up a lot of Runnables to execute. They will run regardless of whether or not your Thread is running because they've been queued up to run on the main thread. Thread.sleep(100) or Thread.sleep(1000) doesn't have this issue simply because you're giving the system enough time to execute all color commands. However, it is still possible to have this issue if you pressed the off button at just the right time.

Upvotes: 1

Related Questions