• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Understanding thread interference

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


------------------------------
OUTPUT

Hey!
Thread Thread-0 runnnig.
You invoked the static logger.
Thread Thread-1 runnnig.
You invoked the instance logger.

------------------------------

Here's a few thoughts i want to clear out:

First a couple of links :

------------------------------------------------------------------------------------------
webpage

Point of focus : Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap.

webpage

Point of focus : Memory consistency errors occur when different threads have inconsistent views of what should be the same data.
------------------------------------------------------------------------------------------

The links above explain two common issues faced with threads.

I have executed my code to get the output above. All i wanted to understand is that would it be wrong for me to expect an output like this sometime. The idea behind this thought is "Since i am executing two threads with the same object and not marking any method as synchronized, it may be possible during some 'random' invocation of the program for both threads to read the staticInvoked variable as 'false' ".

Is the above train of thought plausible?

------------------------------
HYPOTHETICAL OUTPUT

Hey!
Thread Thread-0 runnnig.
You invoked the static logger.
Thread Thread-1 runnnig.
You invoked the staticlogger.

------------------------------
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I would say quite plausible. If you put a Thread.sleep() of a half sec or so in the static logger before it changes the variable to true, I would imagine you would see that output.

So without the sleep statement, all that would be required is that the VM, for whatever reason, decided the first thread's slice was over with and it was time to give the other thread some time, and run the other thread long enough for it to read the variable.

rc
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the effect the call to sleep() would cause. What i was wondering was could the code unchanged give me a different output from what i received. I guess your reasoning of time slicing would cause that to happen right?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shridhar Raghavan wrote:I get the effect the call to sleep() would cause. What i was wondering was could the code unchanged give me a different output from what i received. I guess your reasoning of time slicing would cause that to happen right?


Yes, without any change, the results could be different and cause the problems you describe.

Time slicing is part of it, but also understand that the two threads could be running on different processors at the same time, so when the are both run the sequence of calls can be completely un-related to each other without the need to switch thread context or sleeping. Practically, on your machine and the same version of Java, and on simple code like this you will usually get the same results over and over again (until something else happens to play with your processor and interfere with the operation). But run it on a different computer, different version of Java, or run more complex code and all kinds of variations happen unless you force synchronization.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic