5

在 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
4

2 回答 2

3

Passing Thread object as argument to DoEvery function makes no sense because you immediately overwrite it with Thread.new, check out this modified version:

def DoEvery(intervalSec, durationSec)
    thread = Thread.new do
        start = Time.now
        Thread.current["stop"] = false

        timeTakenToComplete = 0
        loopCounter = 0
        while(timeTakenToComplete < durationSec && loopCounter += 1)
            if Thread.current["stop"]
              Thread.current["stop"] = false
              puts "paused"
              Thread.stop
            end

            yield

            finish = Time.now

            timeTakenToComplete = finish - start

            sleep(intervalSec*loopCounter - timeTakenToComplete)

        end
    end
    thread
end

# User input loop.
exit = nil
while(!exit)
    userInput = gets
    case userInput
    when "start thread\n"
        sample500 = DoEvery(0.5, 30) {File.open('abc', 'a') {|file| file.write("a\n")} }
    when "pause thread\n"
        sample500["stop"] = true
    when "resume thread\n"
        sample500.run
    when "exit\n"
        exit = TRUE
    end
end

Here DoEvery returns new thread object. Also note that Thread.stop called inside running thread, you can't directly stop one thread from another because it is not safe.

于 2012-11-05T07:25:59.570 回答
2

您可能能够更好地使用Ruby Fiber对象来完成您正在尝试的事情,并可能在正在运行的系统上实现更高的效率。

Fiber 是在 Ruby 中实现轻量级协作并发的原语。基本上,它们是一种创建可以暂停和恢复的代码块的方法,就像线程一样。主要区别在于它们永远不会被抢占,并且调度必须由程序员而不是 VM 来完成。

请记住,MRI Ruby 的当前实现不提供任何并发运行的线程,您能够完成的最好的工作是绿色线程程序,以下是一个很好的示例:

require "fiber"

f1 = Fiber.new { |f2| f2.resume Fiber.current; while true; puts "A"; f2.transfer; end }
f2 = Fiber.new { |f1| f1.transfer; while true; puts "B"; f1.transfer; end }

f1.resume f2 # =>
  # A
  # B 
  # A
  # B
  # .
  # .
  # .
于 2012-11-05T05:08:01.277 回答