• 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

syncronization is a problem, is it not?

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saturday, February 14, 2004
I wrote this code to prove
“In the synchronized block of statements,If instead of a this reference, the expression yielded a reference to another object, the lock associated with that object would be acquired before the thread continued.”
psedocode:
--create a thread in which pass a runnable object of the Funda() class
--run() method calls the synchOther() method which will get the lock on a object of Harry class(har) and goes to sleep for 10 seconds.
--Inside the main() thread now call the name method of the harry() class which should not run since the Thread is holding the lock on har object.

My problem is that in the main method as soon as I call the name() it runs. I hope that I am not making any basic mistakes.Plz make the necessary changes so that the code proves the aforesaid objective.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the java language spec,

Acquiring the lock associated with an object does not of itself prevent other threads from accessing fields of the object or invoking unsynchronized methods on the object

(JLS - Synchronized statement
So I think what you need to do is make the name() method of Harry synchronized (or have it contain a synchronized block)
 
I didn't say it. I'm just telling you what this tiny ad said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic