0

I have a piece of code I need to lock so that only one thread can enter the critical area at a time. My program is written in Kotlin and run on Android.

I have declared a singleton like so

object MySingleton {
   fun critical() {
      synchronized(this) {
         // critical section here
      }
   }
}

My question is - is this correct in that this will definitely only allow just one thread in?

zaitsman
  • 8,984
  • 6
  • 47
  • 79
  • 1
    Many of the concerns expressed in https://stackoverflow.com/questions/442564/avoid-synchronizedthis-in-java also apply to Kotlin objects. – Klitos Kyriacou Feb 07 '22 at 23:12

0 Answers0