12/30/2023 0 Comments Kotlin by lazy with parameterSo, confused? When to use Synchronized and when to use Publication?īoth the modes provide thread safety, but in case of publication, the initializer is run more than once till the value gets initialized after that the same stored value is returned. Contrary to that, there is no such restriction there in Publication, multiple threads are allowed to run the initializer lambda but the first one to create the instance wins, and that value is published as used by other threads. In the case of mode Synchronized, multiple threads are not allowed to run the initializer lambda, only one thread is allowed inside the monitor, it executes the initializer and other threads use the stored value. We have already talked about 1 and 3, now let’s talk about Publication mode. Lazy.NONE is a good choice for those cases. ![]() ![]() In the context of android, I see lots of variables that I have to initialize eagerly in the lifecycle callbacks, which can be deferred to the point where it is first accessed. When you use the mode NONE, be very sure that you do not have multiple threads competing to initialize the value, otherwise results will be unpredictable!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |