简体   繁体   中英

Member variable visibility between UI thread and initialising thread

I'm currently attempting to implement an initialiser thread during the construction of a view in order to avoid the "unresponsive" message that android issues when the UI thread gets tied up for too long - whilst the initialiser thread is still working the UI thread will display a simple loading message and respond to orientation changes etc, and when the initialiser thread is complete the UI thread will regenerate the view using the resources initialised by the initialiser thread. Whilst I've implemented a volatile boolean in order for the two threads to communicate the current status of the initialisation process, I'm not sure if it is necessary to define all of the initialised resources as volatile because they will not be accessed concurrently by both threads. Will the objects/variables initialised by the initialisation thread be visible to the UI thread once the initialisation thread terminates?

Any advice would be greatly appreciated.

That depends a bit how you use the volatile boolean. It does establish a happens-before relationship, so that:

volatile boolean done;
...

doStuff();
done = true;

When done = true change is visible in the other thread, the changes done before (in doStuff() ) are guaranteed to be visible too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM