Skip to main content

Improvements in Multi-Threaded Code in Java and Android

· One min read

We have made some minor improvements to synchronization in our Java and Android SDKs. To benefit from these improvements, please generate your SDKs again.

The first change is that our Java and Android SDKs previously synchronized on non-final fields. This did not cause any issues as the reference was never reassigned but, following good and safe practice, we are now synchronizing on final fields.

The second change has to do with how we are synchronizing on the field. In places where synchronization was being used, our old code would lock every time the user tried to access the instance.

synchronized(myLock) {
if (null == myObject) {
myObject = new MyObject();
}
}

We have switched to the following code which only locks the first time the object instance is accessed.

if (null == myObject) {
synchronized(myLock) {
if (null == myObject) {
myObject = new MyObject();
}
}
}