FizzBuzzGame | Practising TDD swift , building a FizzBuzz game | Unit Testing library
kandi X-RAY | FizzBuzzGame Summary
kandi X-RAY | FizzBuzzGame Summary
Practising TDD swift, building a FizzBuzz game
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of FizzBuzzGame
FizzBuzzGame Key Features
FizzBuzzGame Examples and Code Snippets
Community Discussions
Trending Discussions on FizzBuzzGame
QUESTION
For educational purposes I am implementing classical "fizz buzz" problem using multiple threads.
"fizz buzz" game is:
The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizz buzz
In my implementation I have 4 threads:
- First thread prints number if it is not multiple of 3 or 5 and increment current counter.
- Second thread prints “fizz”...
- Third thread prints “buzz”...
- Forth thread prints “fizz buzz”...
I don't use any locking and thread synchronizations mechanisms. Is my multi-threaded "fizz buzz" implementation thread safe? And, if not, why? I added comments in code of implementation for "suspicious" places.
My Implementation:
...ANSWER
Answered 2018-Nov-01 at 15:11It is not race-free (which is what I think you are really asking), because the threads read from currentNum
when it has been written by another thread, without any synchronisation. Each thread is not guaranteed to see the latest value - each thread will see either the value it last wrote itself, or any value that any other thread has written since.
What this could mean is that you end up with no forward progression in any thread, since each thread may simply fail to see the change made in any other. You could use an AtomicInteger
to solve that problem.
I'm not sure, also, whether the effects of this.currentNum++;
are guaranteed to be seen to other threads as ordered in the same way that they are in the source thread. I suspect that theoretically, the output and increment could be re-ordered, for example:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install FizzBuzzGame
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page