aspose file tools*
The moose likes Beginning Java and the fly likes Please explain this sentence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please explain this sentence" Watch "Please explain this sentence" New topic
Author

Please explain this sentence

Sanjeev Charla
Ranch Hand

Joined: Apr 02, 2009
Posts: 90

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sanjeev Charla
Ranch Hand

Joined: Apr 02, 2009
Posts: 90

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain this sentence
 
Similar Threads
See this thread interaction code
Note on Thread
Found problem in 1.5 book example
synchronized block