在ruby中,是否有可能导致线程从另一个并发运行的线程中暂停。Ruby暂停线程
下面是我迄今为止编写的代码。我希望用户能够键入'暂停线程'和sample500线程暂停。
#!/usr/bin/env ruby
# Creates a new thread executes the block every intervalSec for durationSec.
def DoEvery(thread, intervalSec, durationSec)
thread = Thread.new do
start = Time.now
timeTakenToComplete = 0
loopCounter = 0
while(timeTakenToComplete < durationSec && loopCounter += 1)
yield
finish = Time.now
timeTakenToComplete = finish - start
sleep(intervalSec*loopCounter - timeTakenToComplete)
end
end
end
# User input loop.
exit = nil
while(!exit)
userInput = gets
case userInput
when "start thread\n"
sample500 = Thread
beginTime = Time.now
DoEvery(sample500, 0.5, 30) {File.open('abc', 'a') {|file| file.write("a\n")}}
when "pause thread\n"
sample500.stop
when "resume thread"
sample500.run
when "exit\n"
exit = TRUE
end
end
“我是Ruby的新手,只能学习几天” - 而且你已经在线程中了?这很让人佩服! –
哈哈谢谢!看起来像一个非常好的语言;我喜欢整块东西和屈服! –