• 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 methods

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question regarding jqplusv6.

There is a following code:




The answer is that nothing can be surely said about this code (like it will keep on printing same values for x and y or will always increment these values by 1 etc.)

What I don't understand is the explanation:

"You may be tempted by the synchronized keyword on the run method. But note that, there are two diferent thread objects and both the threads are accquiring locks for their own thread object. So, in this case synchronized doesn't make any sense."


I started to wonder, if that's true, then when there is a sense to make the method synchronized?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make it easier to discuss, let assume the following changes:


What the explanation was saying is "the synchronized method when run will check the lock against the instance of the object itself". In this case you have instance testAand testB.

Usually synchronized method is perfectly fine because you put it on to prevent concurrent access to data that belong to that object instance.

In your case you are manipulating static x and y, which belong to the Test.class itself .. not instance of Test. So your synchronized method is guarding the wrong door. That is why it does not make sense.

If you want to guard concurrent access to static variables, then you need to move the synchronized inside and use Test.class itself or another static variable object as the key.
[ April 18, 2008: Message edited by: Wirianto Djunaidi ]
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if x any y would not be static and both threads testA and testB would try to change these variables in, let's say threadA, then the synchronized methods would have a sense?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai! Djunaidi!

could you please modify the above code and give concurrent protection to static variables.

Please explain.Thank you.
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I guess that the static variables will receive the protection if the fragment of the code that operates on them will be synchronized i.e. on the Test class object:



This way we synchronize this code on the one object, not on the several objects.
[ April 20, 2008: Message edited by: Ismael Upright ]
 
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

This way we synchronize this code on the one object, not on the several objects.



And don't forget to remove the previous synchronization (of the run() method).

Henry
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ops, true
fixed
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot find a way to edit my post..
So here's the code:

...

Okay, found it
[ April 20, 2008: Message edited by: Ismael Upright ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic