> Your assignment for this quiz is twofold: > > 1) Devise an "interesting" definition for sleep which allows negative > durations. Alternately, use one of the definitions given here. This is what negative sleep basically meant for me: if we are negative sleeping, in relation to other threads, then do not wait for those other threads to finish first. Very negative sleepers are very awake and process more often as compared to not so negative sleepers. So a way to think about this is a negative awake setting. With this in mind, this seems like it is really the same as thread priority, set to the negative of the sleep value. I came to this by thinking about this in the approach of how sleep is oftentimes used (to wait for other threads to complete) and thought about what the opposite behavior should be. Note: Like normally, other threads need to play nice and sleep sometimes in order to allow other threads to process, and thusly to allow those others to negative sleep as well. > 2) Write some Ruby code demonstrating behavior which satisfies that > definition. As with the above example, you needn't provide a > drop-in > replacement for Kernel.sleep. module Kernel def n_sleep(n_sleep_time) Thread.current.priority = -n_sleep_time end end # test stuff if __FILE__ == $0 Thread.new { n_sleep(-3); 1.upto(10) {print "A"; sleep(0.1)} } Thread.new { n_sleep( 1); 1.upto(10) {print "B"; sleep(0.1)} } Thread.new { n_sleep(-2); 1.upto(10) {print "C"; sleep(0.1)} } n_sleep(10); 1.upto(10) {print "m"; sleep(0.1)} loop {break if Thread.list.size == 1} end -- Posted via http://www.ruby-forum.com/.