开发者

Concurrency issue with Android

开发者 https://www.devze.com 2023-04-04 02:57 出处:网络
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):

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消