I want to make a timer in Ruby, where after a certain amount of time that the user chooses, the timer rings, or a message pops up. Also, would it be possible to make the timer ring until the user does a certain thing (such as a math problem)?
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- gem cleanup shows error: Unable to uninstall bundl
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
- ruby - simplify string multiply concatenation
Use threads:
Three threads are running in this code:
user_input
thread, which gets a string from the user and sets itsvalue
thread variabletimer
thread, which sleeps for three seconds and then kills theuser_input
threaduser_input
thread to finishWithout the
timer
thread, the code would appear to work exactly like a single-threaded one that prompts for user input and then continues. The execution of the two threads is serialized using #join. The main thread gets the result of the user interaction by looking at theuser_input
'svalue
thread variable.The addition of the
timer
thread causes theuser_input
thread to terminate early (3 seconds in this case). When this happens, theuser_input
thread has not set itsvalue
thread variable, and so returns nil when the main thread interrogates it for this variable. This is how the main thread determines whetheruser_input
terminated due to accepting input from the user, or being killed by thetimer
thread.My way :
That display : 1 2 3 ... 10
Somethink like this?