Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-thread safe #25

Open
krpot opened this issue Apr 5, 2019 · 2 comments
Open

Multi-thread safe #25

krpot opened this issue Apr 5, 2019 · 2 comments

Comments

@krpot
Copy link

krpot commented Apr 5, 2019

I like your library and it's very readable and use to fun. One thing I am wondering is if there is any multi-thread issue because your implementation has one instance.

internal val calendar: Calendar by lazy {
Calendar.getInstance(DateHelper.getSydneyTimeZone())
}

From my understanding, lazy it doesn't create more than one instance and when thread1 and thread2 tries to update date, it think there could be a race condition. What do you think?

@urgentx urgentx assigned urgentx and unassigned urgentx Dec 1, 2019
@urgentx
Copy link
Collaborator

urgentx commented Jan 1, 2020

Kotlin's lazy delegate is synchronized by default: multiple threads will see the same value.

@urgentx urgentx self-assigned this Feb 11, 2020
@urgentx
Copy link
Collaborator

urgentx commented Feb 11, 2020

@krpot However yes; you are right, since the underlying Calendar instance is shared and is not thread-safe. Perhaps it would be better to use Calendar.getInstance() every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants