• 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

JDiscuss Test5 Question

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


What will the above code print?

Options

Select 1 correct option.

1.It will keep on printin same values for x and y incrementing by 1 on each line.
2.It will keep on printin same values for x and y but they may be incrementing by more than 1 on each line.
3.It may print different values for x and y but both the values will be incrementing by 1 on each line.
4.You cannot say anything about the values.

The answer given is option 1. I think the answer is option 4.

Please give your suggestion
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option 1 is correct.

Both the incrementation and the printing operations are in the same synchronized block. As such it is like an atomic operation. If you increment, then you have to print.

At any given time since only one thread can increment x and y, and since you are incrementing in steps of one, the correct choice would be option 1 (assuming x and y were initialized with the same value)
 
reply
    Bookmark Topic Watch Topic
  • New Topic