Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic