I got an exercise:
Print in order all positive integers from 1 to 100.
Using blocks, semaphores or other similar mechanisms (but avoiding sleep), coordinate two threads such that the combined output from both threads appears in numerical order.
Sample Output In thread one: The number is ‘1’ In thread two: The number is ‘2’ In thread one: The number is ‘3’ In thread two: The number is ‘4’
The exercise is for Ruby, but I'd like to show to my class that Clojure could be a good option for the task.
I don't have any experience with threads in any language, but I was thinking to use something like:
(def thread_1 (future (swap! my-atom inc) ))
(def thread_2 (future (swap! my-atom inc) ))
but @thread_1 always returns the same vale. Is there a way to coordinate two threads in Clojure?
I found this example in Java using ReentrantLock and Condition, and now I'm trying to translate it to Clojure.