• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please explain this sentence

 
Ranch Hand
Posts: 90
Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These below lines are from Sun's tutorial . . .

Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock:


Please explain the use of this in the above code. . .
Thank you.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this refers to the current object, the object that the method is called on.

Here is a recent discussion with more explanation about "this".

When you use the synchronized keyword with a block, like in the example above, you must specify an object to synchronize on. In the example, you are synchronizing on the object that the method was called on. You can also choose to synchronize on any other object.

Note that using the synchronized method on a method:

is just shorthand for the following:

So, when you use synchronized on a method, you are also synchronizing on the object that the method is called on.
 
Sanjeev Charla
Ranch Hand
Posts: 90
Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, when you use synchronized on a method, you are also synchronizing on the object that the method is called on.



synchronizing a method gives . . .
1. no interleaving between threads,
2. it automatically establishes a happens-before relationship.

but, what is synchronizing on the object ??

Thanks for the response.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a method is synchronized, it also synchronizes on the object the method is called on (or the object's Class object if the method is static). The only difference between an explicit synchronized block on this and a synchronized method is that with the former, you can have code inside the method that's not synchronized. The call to nameList.add(name); from your code is a good example; this code will now be executed without any synchronization, whereas if the entire method would be synchronized it would be executed with synchronization.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronization always happens on an object.

Each Java object has a lock associated with it. When you use that object in a statement like this:

then in the first line, the first thread that executes the code will be able to get the lock of the object. In the last line, at the end of the block, it will release the lock. If another thread in the meantime also wants to execute the block, then in the first line it has to wait, because the lock is already taken by the first thread. The second thread will wait until the lock is released again by the first thread.

See this tutorial for a detailed explanation of how synchronization works: Synchronization tutorial.
 
reply
    Bookmark Topic Watch Topic
  • New Topic