在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的新手,只能學習幾天」 - 而且你已經在線程中了?這很讓人佩服! –
哈哈謝謝!看起來像一個非常好的語言;我喜歡整塊東西和屈服! –