• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
shankara me
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic