This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized block" Watch "synchronized block" New topic
Author

synchronized block

shankara me
Greenhorn

Joined: Nov 30, 2012
Posts: 14
the general form of synchronized block is

Here obj means object of table class or object of any class.
1)If we write this in place of obj we get lock on the object of table class.
That means we can’t access the synchronized block simultaneously by same object of table class.
2) But if write other object’s name other than table class in place of obj,what it means?
Then which object can’t access the synchronized block simultaneously?

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

shankara me wrote:the general form of synchronized block is

Here obj means object of table class or object of any class.
1)If we write this in place of obj we get lock on the object of table class.
That means we can’t access the synchronized block simultaneously by same object of table class.
2) But if write other object’s name other than table class in place of obj,what it means?
Then which object can’t access the synchronized block simultaneously?



You can use any class object for lock.

this refers to current executing object.

Whatever class object or this keyword you will use, it will be locked at execution time,there is nothing different theory for this keyword and other class object over there.


My one regret in life is that I am not someone else.
- Woody Allen
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186

1)If we write this in place of obj we get lock on the object of table class.
That means we can’t access the synchronized block simultaneously by same object of table class.

If you write "this" you will use this specific instance of the object of the table class for the lock.


2) But if write other object’s name other than table class in place of obj,what it means?
Then which object can’t access the synchronized block simultaneously?


In order to lock on any object, it has to exist. So you can't just write an arbitrary name in the synchronized block.


Finally, objects do not access synchronized blocks, threads do. So, using a synchronized code block prevents multiple threads from accessing the block of code simultaneously.
Diplav Srivastava
Greenhorn

Joined: Aug 17, 2011
Posts: 3

the synchronized is used in multithreaded environment.if Table class object or any other object is accessed by say two threads then inside the synchronized block only one thread has access.i.e.the lock is with one of the thread.so it(the thread with lock) can do anything with the table object or any other object inside the block.when this thread come out then other thread will go inside and perform operations on that object.
'this' simply refers the current object(but just an object).
 
 
subject: synchronized block
 
Similar Threads
synchronized(obj)
Synchronized Methods and Synchronized Blocks
doubt on synchronized block in java
semantic of synchronization
doubt on synchronized block-3