• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Difference in behaviour of while loop in Threads when loop body contains code v/s when it doesn't

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Question: Why does the loop gets stuck in case-1 whereas in case-2 it terminates after some execution time? The only difference I've done is including some line of code inside while..loop.
The sample code is taken from video @ Concept behind volatile keyword in java

case-1:

case-2
 
Marshal
Posts: 70318
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, but both loops do terminate. Adding discussion to our threads forum.
 
Saloon Keeper
Posts: 12270
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's implementation dependent.

The JVM is free to ignore any changes made to a variable in a different thread unless execution between the threads is synchronized.

It turns out that the particular JVM you are running the code on decides to synchronize the variable state when you perform an I/O operation, because it probably doesn't have anything else to do in the mean time.
 
Lokeshwar Tailor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell - Strange, as per Stephan's explanation, it must be JVM dependent. My execution environment is as below, executing scripts through the command-line.:
OS: Ubuntu 18.04.3 LTS
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
javac 1.8.0_221


@Stephan - Even I suspected the same.
> The threads are not communicating with the main memory when there is nothing to execute.
> Once the loop has something to execute, they communicate with the main memory, after a certain point, and are made aware of the latest changes in variables variable, and, hence termination happens.
 
Stephan van Hulst
Saloon Keeper
Posts: 12270
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have it the wrong way around.

In the first snippet, the thread is constantly busy (doing nothing). It's called a busy wait, and it will cause your CPU to get very hot.

In the second snippet the thread is actually much less active. It triggers an I/O operation and then possibly waits for an interrupt to notify it that I/O is done. It could be that while the thread is waiting for I/O to be done, the JVM takes the opportunity to synchronize thread state with main memory.
 
Lokeshwar Tailor
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification @Stephan
 
There is no "i" in denial. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic