• 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

Threads

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

This is one of the questions from Dan's mock exam


The answer to the question is given as "ABC" and I got the same answer after executing

But I am still unable to understand.
The variables sa's declared in class A and class B are different. In class B the synchronisation is done on static variable of B. while in class A it is on A.sa. Then why will the thread t1 wait for the synchronisation on sa(of class B) to complete before entering in to the synchronisation block of sa(of class A) .

And also how will the sa of class A get the values {a,b,c} when it is assigned to {x,y,z}. After assignment there is no direct refernce of sa(of B) to sa(of A)

Please help me understand this.

Thx
Sharanya

Edited by Corey McGlone: Added CODE Tags
[ August 05, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The objects that are being synchronized on are not different objects. They are, in fact, one in the same. Sure, there are multiple references to that object, but, in the end, there is only one object. Every object has just one lock that can be acquired for synchronization so this synchronization works. Let's look at this using pictures for a moment (please forgive the poor ASCII art).

When we start (this would be at the start of the main method), we have this:



As you can see, we our class, B, which contains a static reference variable, sa. That variable refers to an array, as shown above.

Now, when we execute the first line of our main method (creating a new A object), we have a picture that looks like this:



From our picture, you can see that both B.sa and A.sa refer to the same Array object. How did it get this way? Well, when we invoked the constructor for A, we passed to it a reference to our array. In that constructor, we assigned that reference to the member variable A.sa. That's how we got to this point.

Of course, the main thread already holds the lock of our array object so, when we invoke A.start(), there's nothing that thread can do except wait. That gives the main thread the ability to modify the array before it can be printed by the new thread. That, of course, leaves us with a picture that looks like this:



Now that the main thread is done, it releases its lock on the array object. At that point, the new thread can continue and it prints out the contents of the array, which is now "ABC".

I hope that helps.
 
Sharanya Sharma
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yae that really helped...
Thanks for the response

-Sharanya
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic