File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 9: syncronized(this) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chapter 9: syncronized(this)" Watch "Chapter 9: syncronized(this)" New topic

Chapter 9: syncronized(this)

Graciela Zaffarana

Joined: Jan 09, 2010
Posts: 17
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.

Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

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
Both the above codes will work similar to this now i.e. the synchronization will not work...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
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...


Graciela Zaffarana

Joined: Jan 09, 2010
Posts: 17
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!

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
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

Joined: Jan 09, 2010
Posts: 17
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,

I agree. Here's the link:
subject: Chapter 9: syncronized(this)
Similar Threads
Thread causing Hang ??
wait() causing problems
After notify()
synchronize (this) - what does it mean?
Interaction between threads