File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Understanding thread interference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Understanding thread interference" Watch "Understanding thread interference" New topic
Author

Understanding thread interference

Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71


------------------------------
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.

------------------------------
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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

Joined: May 27, 2010
Posts: 71
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?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
 
 
subject: Understanding thread interference