• 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

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

It's a little bit complex example, but someone can explain me why this code doesn't seem to be synchronized although it should be?

Indeed, when I run it, the output is:
Thread1-989, -237
Thread2-989, -487
As I interpret, this happens:
Thread1 run() executes this: rect.width = rect.width - 500 = 11 -500 = -489;
Thread.yield() -> Thread1 must not move out of the virtual CPU because of synchronization
Thread1 continues: rect.height -= 13 => rect.height = 13 - 250 = -237;
now Thread2 begins its execution:
rect.width = rect.width - 500 = -489 -500 = -989;
and just after that Thread2 moves out the CPU so that Thread1.run method can execute:
System.out.println(getName() + ss.rect.width + ss.rect.height);
=> Thread1 -989 , -237
but how is it possible since the block where width and height are set is supposed to be synchronized and that lock can't be given up in the middle?
I'm perplexed of what really happens here,
I would be very grateful for any help,
Cyril.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(getName() + ss.rect.width + " , " +ss.rect.height);
The fields width and height of class Rectangle are being accessed outside of any synchronized methods.
If one thread has a lock on an object, other threads can access the fields of the object directly or through unsynchronized methods without first acquiring the lock on the object.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene...
Of course, you're right!
I've forgotten that a Thread that had a lock could be on competition not only with other synchronized blocks but others threads too!!!
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
temporary memory overload! I suppose
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
too complex and technical
 
reply
    Bookmark Topic Watch Topic
  • New Topic