Last active
March 5, 2016 12:45
-
-
Save rkotov93/7aa09af1e9955a72f215 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def random_sum | |
| Thread.current[:output] = "" | |
| 10.times do | |
| a = Random.rand(1..1000) | |
| b = Random.rand(1..1000) | |
| Thread.current[:output] += "#{a} + #{b} = #{a + b}\n" | |
| end | |
| sleep 3 | |
| end | |
| def fact(n) | |
| n == 0 ? 1 : n*fact(n-1) | |
| end | |
| def random_fact | |
| Thread.current[:output] = "" | |
| 10.times do | |
| x = Random.rand(5..10) | |
| Thread.current[:output] += "#{x}! = #{fact(x)}\n" | |
| end | |
| sleep 3 | |
| end | |
| t1 = Thread.new { random_sum } | |
| t2 = Thread.new { random_fact } | |
| t1.join | |
| t2.join | |
| puts t1[:output] | |
| puts '===' | |
| puts t2[:output] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment