| Author |
synchronized method
|
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
Hi, this is from K&B. How can we say synchronized (args) here at Line 3? I thought it should be either synchronized (this) or synchronized (P763_Question05.class). Please advice
|
OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2927
|
|
Harikrishna Gorrepati wrote:Hi, this is from K&B. How can we say synchronized (args) here at Line 3? I thought it should be either synchronized (this) or synchronized (P763_Question05.class). Please advice
Is it from the perspective of avoiding Deadlock? if not, then as args is also an object, it can be used right?
|
Mohamed Sanaulla | My Blog
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
If we can get lock on any object, Please advice what is the difference between synchronized(args) and synchronized(this)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16690
|
|
Harikrishna Gorrepati wrote:If we can get lock on any object, Please advice what is the difference between synchronized(args) and synchronized(this)
Different objects?
And in your example, the second case will not work, as static methods don't have access to a "this" object.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2927
|
|
I was trying some code example to show the difference. Came up with one-
You can see the difference in the output- the way myMethod1 increments and prints in both the cases.
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
You are right. I meant to ask something like this. Let me change my question. Assuming that there is no "static" context, Please advice what is the difference between synchronized(args) and synchronized(this). Here is the code.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2927
|
|
Harikrishna Gorrepati wrote:You are right. I meant to ask something like this. Let me change my question. Assuming that there is no "static" context, Please advice what is the difference between synchronized(args) and synchronized(this).
If you try to execute the above code which I tried-
with synchronized(args)
One of the output:
In myMethod1 1
In myMethod2 4
In myMethod1 2
In myMethod2 7
with synchronized(this)
one of the output:
In myMethod1 1
In myMethod2 3
In myMethod1 5
In myMethod2 7
You can see that- the second set of output is clearly what we were expecting.
or better, you can make the following changes-
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Harikrishna Gorrepati wrote:You are right. I meant to ask something like this. Let me change my question. Assuming that there is no "static" context, Please advice what is the difference between synchronized(args) and synchronized(this). Here is the code.
As, Henry said, Both the code snaps are synchronized on different objects. synchronized(this) means, you are synchronizing the below code snaps on the currently executing object. And, you can specify any other object instead of this currently executing object. In the second case, the below code snaps are synchronized on the args object.
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2927
|
|
|
I just wrote a blog post related to this. Might be helpful.
|
 |
 |
|
|
subject: synchronized method
|
|
|