• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

synchronize on local variables of an object reference

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is Also, from Dan's mock exam on Thread...


It is either not possible or not advisable to synchronize on which of the following?
a. primitives
b. A member variable that is an object reference.
c. A local variable that is an object reference.
d. A randomly selected object.


answer is: acd.
a is obviously one of the answer, but why c and d?
Any examples showing the reason for c and d are appreciated.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object that is used to synchronize two or more threads must be shared by all of the threads otherwise the lock associated with the object will not be shared.
Each time a method is invoked a new set of the local variables are created. If the method is recursive and invokes itself 10 times then 10 sets of the local variables are created. Similarly, if ten threads invoke the same method then each will have a unique copy of the local variables. Since each unique copy has its own lock it is not possible to use the unique copies of local objects for synchronization.

If synchronization had worked properly then it would print A1A2A3B1B2B3 or B1B2B3A1A2A3. Since the local variable does not provide proper synchronization the output is more likely to be something like A1B1A2B2A3B3.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an interesting variation of the previous example. Why does the following provide effective synchronization?
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it because the String Object of the empty string "" is used for all the variables because of compiler optimization???
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dan,
"" is created in the String pool and therefore created only once.
Remember that String literals are not garbage collected, and at the end of the run method will remain in the string pool.
Intersting point though
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alfred and Shishio,
Yes, you are both correct: String literals are stored in the constant pool and are shared.
I think I'll add those questions to my exam.
 
Get me the mayor's office! I need to tell her about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic