• 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

Why am I not getting incorrect output?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a topic -- of someone else trying the same thing that you are trying...

https://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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I will check that out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic