Glitch
Glitch

Reputation: 2785

Concurrency issue with Android

I'm having a bit of trouble with concurrency. The situation is as follows:

I have a method which adjusts brightness like so (it is executed in the UI thread):

public void adjustBrightness(final float brightness) {
    window_object.setBrightness(brightness); 
    window_object2.setBrightness(brightness);
}

The setBrightness method called on those objects is the second block of code in this question: Clean way to implement gradual fading of brightness in Android?

As you can see, that block of code executes within another thread. This is problematic, because it means that setBrightness returns as soon as the thread is started, causing window_object2 to adjust its brightness whilst window_object is still adjusting. I do not want them to execute concurrently!

How can I enforce sequential execution of these methods such that they do not interlace? Keep in mind I need methods which are considered 'safe', so I don't get obscure concurrency bugs.

Thanks.

Upvotes: 0

Views: 205

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006644

Simple: delete the second line.

You only have one screen. The code you linked to affects the brightness for the whole screen. Hence, you only need to run that code once.

That code you linked to is rather inefficient, BTW -- use postDelayed() and get rid of the background thread.

Upvotes: 1

Related Questions