• 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

Exam LAb Threads Problem-2

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


In the exam lab explanation ,it's written that doVIP would be called 3 times by each thread
the output is:
000000


i cannot get how is this output occuring ..
iand each thread is assigning it's own value ti
static volatile int x=0;
how is all zero's possible in output ?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Omg, is volatile even there in the syllabus? I don't remember seeing it in Kathy Sierra and Bert Bates.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x is static, so every instance of the class will share its value. Since the last constructor call will set the value of x to 0, so the output will contain only 0s.

Rohit, volatile is very simple, it just tells the JVM that the variable's value changes very frequently so JVM should not cache the value of this variable...
reply
    Bookmark Topic Watch Topic
  • New Topic