• 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

In this volatile example program where are 2 threads created?

 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We know that:
The Java volatile keyword guarantees visibility of changes to variables across threads.

In the below example code there is only 1 thread being used. How is this an example of volatile keyword when there is only a single thread being used?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raja singh kumar wrote:. . .  How is this an example of volatile keyword when there is only a single thread being used? . . .[

To the Java® Language Specification (=JLS); also read §17.4.5. Oddly enough the Java10 edition of the JLS doesn't seem to have an index; in the Java8 edition it also lists this part under “volatile”. You will see that writes and reads to a volatille variable happen‑after previous reads and writes. The happens‑after relationship applies irrespective of ho wfew or many threads are running, but you will only notice it if you have multiple threads.
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two threads: the main thread and the thread you started.

The main thread calls shutdown() which writes to the running variable, while your new thread reads it.
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:There are two threads: the main thread and the thread you started.

The main thread calls shutdown() which writes to the running variable, while your new thread reads it.



Thank you
reply
    Bookmark Topic Watch Topic
  • New Topic