do these behave the same? ```object Foo { @Synch...
# getting-started
y
do these behave the same?
Copy code
object Foo {
  @Synchronized
  fun f() {
    doThing1()
    doThing2()
    doThing3()
  }
}
vs.
Copy code
object Foo {
  fun f() {
    synchronized(this) {
      doThing1()
      doThing2()
      doThing3()
    }
  }
}
c
Yes. Because that is an
object
the
synchronized(this)
is over the single instance, which mirrors what @Synchronized would do for a static method.
thank you color 1
c
Not exactly (unless compiler optimizes it): the first example creates a single callstack frame as it get created inside the synchronized code. The second sample creates separate callstack frame for each call in each thread and only then synchronizes the call. Also, this example very simple, but later, modification in the first example cannot be outside synchronized block. This is difference in the design of the code.
c
The ask was whether they both behave the same - they do, despite technical differences.