aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronize an instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronize an instance variable" Watch "Synchronize an instance variable" New topic
Author

Synchronize an instance variable

W Lin
Greenhorn

Joined: Mar 16, 2012
Posts: 11



There are 3 "MyRunnable" instance, and "i" is NOT a static attribute, so they use different lock, BUT WHY the code output like this:



If I change "synchronized(i)" to "synchronized(this)", it works as expected:



I don't understand the first output...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

In the first example (which is the one you're asking about, right?) the three threads synchronize on different objects. If I'm not mistaken, you already understood this part.

So the three threads are essentially unsynchronized and none of them prevent any other from running at any time. So you shouldn't expect thread A to do anything before thread B does anything, or vice versa. However you're asking a question about that, which seems to imply you are expecting some particular ordering of the output. Can you explain what that ordering is, and why you expect it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39


Hint: it is related to autoboxing and the integer cache.

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
W Lin
Greenhorn

Joined: Mar 16, 2012
Posts: 11
Hi Paul, I don't understand this -- "So you shouldn't expect thread A to do anything before thread B does anything, or vice versa"

I expect the output to be in random order (similar to the 2nd output -- A/B/C in random order), because the 3 threads each locks its own "i" and they run simultaneously.



Paul Clapham wrote:In the first example (which is the one you're asking about, right?) the three threads synchronize on different objects. If I'm not mistaken, you already understood this part.

So the three threads are essentially unsynchronized and none of them prevent any other from running at any time. So you shouldn't expect thread A to do anything before thread B does anything, or vice versa. However you're asking a question about that, which seems to imply you are expecting some particular ordering of the output. Can you explain what that ordering is, and why you expect it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

W Lin wrote:Hi Paul, I don't understand this -- "So you shouldn't expect thread A to do anything before thread B does anything, or vice versa"

I expect the output to be in random order (similar to the 2nd output -- A/B/C in random order), because the 3 threads each locks its own "i" and they run simultaneously.



Paul is actually correct, you shouldn't expect a random order just because the execution is not synchronized..... however, since you added a delay with each iteration (in the example), it is highly unlikely that the output won't interlace..... so, I guess you are both right....

Regardless, for the answer, see my last hint.

Henry
W Lin
Greenhorn

Joined: Mar 16, 2012
Posts: 11
Henry Wong wrote:
Hint: it is related to autoboxing and the integer cache.


OMG, thanks a lot! This is so tricky



the output is:



If I change

to


the output is:



None of the book mentions it. Thanks a lot again!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

Henry Wong wrote:
Hint: it is related to autoboxing and the integer cache.


Ouch.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

Hello Lin,

Just saw your previous reply to this post, and am wondering is it really happening???
I Mean,

private String s = "";
and
private String s = new String("");

are no different. Both will create a new String object on Heap every time a thread is started.... So how can there be difference in output for the two cases???
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

Man you are right.... Its giving that weird output... And I can't figure it out how is it happening...
Can anyone explain this to me, why is there a difference in using both these definition of String variable???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

R. Jain wrote:
Just saw your previous reply to this post, and am wondering is it really happening???
I Mean,

private String s = "";
and
private String s = new String("");

are no different. Both will create a new String object on Heap every time a thread is started.... So how can there be difference in output for the two cases???


This second example is similar. The first example was due to the integer cache. This second example is caused by the string pool.

Henry
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

Henry Wong wrote:
R. Jain wrote:
Just saw your previous reply to this post, and am wondering is it really happening???
I Mean,

private String s = "";
and
private String s = new String("");

are no different. Both will create a new String object on Heap every time a thread is started.... So how can there be difference in output for the two cases???


This second example is similar. The first example was due to the integer cache. This second example is caused by the string pool.

Henry


Ok, Got it...
Thanks.. This thing just slipped away from my mind
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

W Lin wrote:
Henry Wong wrote:
Hint: it is related to autoboxing and the integer cache.


OMG, thanks a lot! This is so tricky



the output is:



If I change

to


the output is:



None of the book mentions it. Thanks a lot again!



Well, K&B does... You might not have noticed clearly..

Anyways... Henry just gave a nice hint.... Let me explain it little more....

The effect you get using: - String s = new String("")
will be same that you get while using: - Integer i = Integer.valueOf(4);

Actually JVM maintains a chache of all integers between the range -128 to 127.. So, you will get the same object everytime you use something like: - Integer i = 5; or, Integer i = new Integer(5);
Same is the case with String s = "Hello", where 's' will be added to the String Pool... And if there is already a literal in the pool with that value... "s" will simply point to the same object....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronize an instance variable
 
Similar Threads
Threads: synchronizing a block of code (K&B 1.5)
Thread Question about sleep method
Threads blocking on static method.... or do they?
join() method in Thread doesn't work as expected
Lock Mechanism Doubt