aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) 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 JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized block" Watch "synchronized block" New topic
Author

synchronized block

Tom Mark
Greenhorn

Joined: Jun 15, 2011
Posts: 27
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 28, 2008
Posts: 5575

for more understanding, try below example:

got it?
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

@Tom

Read about class and instance level locks in Java. Its good from interview perspective also.


Java Experience
jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
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

Joined: Jun 15, 2011
Posts: 27
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 18, 2011
Posts: 59
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

Joined: Jun 15, 2011
Posts: 27
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 15, 2011
Posts: 27
Get it.
Thank you all.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Tom Mark wrote:Thank you all.

Welcome
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

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


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized block