https://github.com/zipcodecore/monkeystypewritersohmy
https://github.com/zipcodecore/monkeystypewritersohmy
corejava corejava-chapter14
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zipcodecore/monkeystypewritersohmy
- Owner: ZipCodeCore
- Created: 2019-03-13T19:48:25.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-04-20T20:20:29.000Z (8 months ago)
- Last Synced: 2025-06-01T01:29:43.552Z (7 months ago)
- Topics: corejava, corejava-chapter14
- Language: Java
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 41
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MonkeysTypewritersOhMy
## Monkey Typewriter
According to Wikipedia:
> The infinite monkey theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite
amount of time will almost surely type a given text.
We don't have that kind of time, but what we do have are super smart monkeys. These monkeys are able to copy text.
So, guess what? We're starting a printing company powered entirely off of monkey typists.
### What to do!
Testing multithreaded applications is SUPER difficult. Even moreso, there's a chance that (if you're not actually
testing things correctly) your tests will occasionally pass when they shouldn't (since a poorly threaded application
isn't guaranteed to mess anything up). Instead, we're going to use the main method in `Monkey Typewriter` to see
exactly what happens when things are threaded incorrectly vs correctly.
### Part 1
Made for you is an abstract base class of `Copier` which has a constructor that takes a String and turns that into
an iterator. This will allow us to traverse the text to be copied and pass it along to each monkey (thread).
Extend `Copier` in `UnsafeCopier`. Then, write a `run` method that will have the monkey grab the next word and append
it to the copy.
Modify `MonkeyTypewriter` to create 5 monkeys (threads) using the `UnsafeCopier` and start them.
After the sleep, print out the results of the unsafely copied passage.
### Part 2
Finish the `SafeCopier` and then call that from the main method, in addition to the unsafe version.