This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Thread Qn from Dan mock exam site

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

It displays ABC.
I did not understand the code.Can anyone please explain ??

Thank you in advance!
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key point is that both threads synchronize on the same object, the 3 element String[] array. The pupose of this is to be sure that references to the string literals "A", "B", and "C" are stored in the array by B.main() before A.run() prints the array.

B.main() takes the lock on the array, using sa as a reference. B.main() then creates an A object, passing the array reference to the A(String[]) constructor. That constructor saves the passed reference to the String[] array in saa.

B.main() then starts the second thread, but that thread immediately blocks on the String[] object lock held by the main thread.

When B.main() finishes initializing the array, it releases the lock and exits, ending the main thread.

A.run() now unblocks and takes the lock on the array. It prints the 3 array elements as ABC and exits, terminating the second thread.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic