• 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

Chapter 9: syncronized(this)

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, see code below.

Locking on Integer object -- either first or redlight -- renders the desired result: each thread completes the 12 iterations before the next thread comes around.

On purpose, I avoided using a for-loop. I am trying to understand what "this" in synchronized (this) means. I do not understand why if I replace either object --- first or redlight --- with "this" and still keep the curly braces arond the same lines of code, I no longer get the desired result. What does "this" mean? I cannot find a case where I can use synchronize (this) effectively.

Thank you.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Graciela the behavior that you are seeing is due to the integer constant pool that java maintains. When an int is boxed into Integer, and its in the range of -128 to 127, then the Integer object is taken from that pool. This is defined in the JLS

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.


So you have three instances of Letter2 class which all have the same instances of first and redlight. That's why synchronization works on them but not on this. Try to modify your code in one of the following manners
OR
Both the above codes will work similar to this now i.e. the synchronization will not work...
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Graciela,




Here three instances of Letter2 are created... so 'this' in each case would be a different object...

Whereas if you had created the threads like



i.e using the Thread(Runnable) constructor, the 'this' refers to the same Runnable on all three threads, and hence locking should work just fine...

Regards,
Vishwa



 
Graciela Zaffarana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, indeed! I can see now how synchronized(this) works just fine using the runnable interface version as opposed to the inheritance version. Gosh! Thank you so much!!!

I can now see how both versions tackle the runnable target; to me, it is easier to follow the "interface version" But, no doubt one needs to fully understand both.

Thank you very much!

Graciela
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, that you got it

I'd recommend reading through the Threads related posts by an old rancher, Maha Anna...

here is one on synchronized blocks

 
Graciela Zaffarana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vishwanath ...

I greatly appreciate your pointers and references. I will follow up on them. ( I was almost losing it yesterday --- not even trusting the "for-loop"! )

Best regards,

G.Z.
reply
    Bookmark Topic Watch Topic
  • New Topic