This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Why am I not getting incorrect output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why am I not getting incorrect output?" Watch "Why am I not getting incorrect output?" New topic
Author

Why am I not getting incorrect output?

simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Just learning about multithreading with Java. I created a small program that uses multiple threads to count up to a number and then stop. I assumed that, because using something like an int variable (say, int count = 0;) and ++ (as in count++) doesn't define an atomic action, I'd have to put all non-atomic actions in synchronized methods or blocks, and declare non-atomic variables as volatile, which I did.

Everything worked. The program counted to whatever I set it to count and stopped. No interleaving or memory consistency errors (not too clear on the difference between those two things yet).

Then, when I took away the volatile and synchronized keywords and ran the program. It worked just the same no matter what the upper count limit was or how many threads were running. I was expecting to get wrong output.

The following is the altered code, with no synchronization. Just wondering if anyone could point to where my thinking is wrong. Thanks.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40


Just because there is a race condition -- doesn't mean that you will get an incorrect answer. Thread race conditions means that stuff can go wrong -- it doesn't mean that it will go wrong, nor will you notice it.

Regardless, here are some suggestions...


* Don't have your threads track the total. For example, have 10 threads increment 1,000,000 times may or may not get to 10,000,000. However, having 10 threads increment until they get to 10,000,000 will definitely get to 10,000,000.

* Have more threads -- and if possible, run it on a multicore/multiprocessor machine

* Have more iterations -- with short runs, the racers may start and finish before the others start.

* Remove non-race condition code -- don't pause or print any output until the very end.


All in all, I think that suggestion one is likely your main culprit.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Thanks Henry.

The main take away for me is the idea you suggested that just because it didn't go wrong doesn't mean it won't go wrong - I'm assuming if this type of thing appeared within more complex circumstances.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Here is a topic -- of someone else trying the same thing that you are trying...

http://www.coderanch.com/t/570718/threads/java/demonstrate-unsafe-thread

At the end of that topic, I modified the code -- in order to purposely get an incorrect total. Perhaps you can try it out?

Henry
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Thanks. I will check that out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why am I not getting incorrect output?
 
Similar Threads
Restrict access to static variable from non static synchronized and static synchronized method
ThreadUnsafe
Synchronizing thread woes!
Whiz Lab MOCK Quesitions on THREADS
Synchronized blocks