• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized method

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we can get lock on any object, Please advice what is the difference between synchronized(args) and synchronized(this)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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-
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wrote a blog post related to this. Might be helpful.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic