In ruby, è possibile causare la sospensione di un thread da un thread in esecuzione simultaneo diverso.Ruby Pausa thread
Di seguito è riportato il codice che ho scritto finora. Voglio che l'utente sia in grado di digitare 'pause thread' e il thread sample500 per mettere in pausa.
#!/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
"Sono nuovo di Ruby, lo sto imparando solo da pochi giorni" - e sei già impegnato? È impressionante! –
Haha grazie! Sembra un linguaggio davvero carino; Adoro l'intera cosa dei blocchi e la resa! –