Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain.
The result of the following code is always: 1 2 3 1 2 3
WHY? I thought the result is unpredictable.
how does synchronized blocks works? I thought synchronized(a) line locks only object on reference "a", but it lock on "b" too.


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Mark wrote: I thought synchronized(a) line locks only object on reference "a"

your thought is correct! except it is not with reference instead with object. you created two threads on single object, hence the two threads share the instance object A. so, if one thread acquired the lock of A object, another have to wait until the former thread release the lock on the A object.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for more understanding, try below example:

got it?
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tom

Read about class and instance level locks in Java. Its good from interview perspective also.
 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i think you are right.

synchronized(a) line locks only object on object 'a', but it din't lock on object 'b'.

to display 1,2,3 the thread object b require lock on 'a'. that is why they display the result in turn.
 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I still don't get it.

Line synchronized(a) locks only A object which means that two threads can't access method void doTask1 concurrently, but they can
access code concurrently in the following code:



because the lock is on A, not on synchronized(this)
That means that the result could be 112233 or else.

I know that I'm wrong, but I don't know why.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Mark wrote:
Line synchronized(a) locks only A object which means that two threads can't access method void doTask1 concurrently

Wrong!


which means if two threads share a common object *a* and if it is synchronized , then both cant enter parallely into the block. one have to wait for other to exit from that block.that's all. *void doTask1 method is not called from synchronized block right?*

you can go through this tutorial
 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually in order to access a portion of code , it does not need the object for the portion of code.

'a' in this case is something like a kind of resource, in order to enter the code portion, you need the lock/ resouce , it does not actually require any specific object to enter, as long as the lock can mutually block (thread) each other. in this case, 'a' object is successfully blocking the two concurrent thread.

this is my rookie explanation. need more expert to clarify . haha..

 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But we can write

and we'll get the same result. Why do we need a?

And what if we try to invoke a.doTask1() method by different threads?
I'm trying to figure out how statement

affects accessibility of object "a" which has method doTask1();
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Mark wrote:But we can write

and we'll get the same result.

Correct! , if you are not consider doTask1() method accessibility!
Tom Mark wrote:
Why do we need a?
And what if we try to invoke a.doTask1() method by different threads?
I'm trying to figure out how statement

affects accessibility of object "a" which has method doTask1();

when the both the thread holds a's lock, no other thread(say example:main thread) can enter into doTask1 *if it is synchronized*
consider below scenario


<edit>corrected typo</edit>
 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get it.
Thank you all.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Mark wrote:Thank you all.

Welcome
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are learning Synchronization , you may find this useful 20 points about Synchronization in Java

Synchronization is tricky and best way to get hold of it to read and implement as much possible. another good reference is "Java Concurrency in Practice book
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic