首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ScheduledTask的execute方法中调用块?

如何在ScheduledTask的execute方法中调用块?
EN

Stack Overflow用户
提问于 2019-09-08 22:28:36
回答 1查看 35关注 0票数 0

我试图在Concurrent::ScheduledTask#execute方法中调用一个块,但是这个块本身从未执行过。

我也尝试了使用Concurrent::ScheduledTask#new方法,但结果是相同的。我觉得这里可能有一个我遗漏的基本问题。任何帮助都将不胜感激!

代码语言:javascript
运行
复制
require 'concurrent'
##
# A basic Event
class Event
  attr_accessor :ticks
  # @param ticks [Numeric] The amount of ticks we wait before executing this event
  def initialize(ticks = 1.0)
    @ticks = ticks
    puts "Event created with #{@ticks} ticks"
  end
  # Calls the block of this event for execution.
  def execute(&block)
    if !block_given?
      raise AbstractEventExecution.new(self)
    else
      Concurrent::ScheduledTask.execute(@ticks *= 0.75) { block.call }
      puts "Executed in #{@ticks} ticks"
    end
  end
end

class AbstractEventExecution < StandardError
  attr_accessor :event
  def initialize(event)
    @event = event
    super("The Event #{event} was not provided an execution block and is abstract.")
  end
end

event1 = Event.new(105)
event2 = Event.new(1000)
event3 = Event.new(50)
event1.execute { puts "hai from event 1" }
event2.execute { puts "hai from event 2" }
event3.execute { puts "hai from event 3" }

输出如下:

代码语言:javascript
运行
复制
Event created with 105 ticks
Event created with 1000 ticks
Event created with 50 ticks
executing an event...
Executed in 78.75 ticks
executing an event...
Executed in 750.0 ticks
executing an event...
Executed in 37.5 ticks

我不确定为什么puts "hai from event x"从来没有出现过。此外,在执行此命令时不会有延迟。应该分别有78.75秒、750.0秒和37.5秒的延迟,但根本没有延迟!

EN

Stack Overflow用户

回答已采纳

发布于 2019-10-16 15:49:02

Concurrent::ScheduledTask#newConcurrent::ScheduledTask#execute都返回一个ScheduledTask对象,然后主线程退出。

这就是为什么"hai from event x"永远不会出现。

代码语言:javascript
运行
复制
require 'concurrent'
##
# A basic Event
class Event
  attr_accessor :ticks
  # @param ticks [Numeric] The amount of ticks we wait before executing this event
  def initialize(ticks = 1.0)
    @ticks = ticks
    puts "Event created with #{@ticks} ticks"
  end

  # Calls the block of this event for execution.
  def execute(&block)
    if !block_given?
      raise AbstractEventExecution.new(self)
    else
      task = Concurrent::ScheduledTask.execute(@ticks *= 0.75) { block.call }
      puts "Executed in #{@ticks} ticks"
      task
    end
  end
end

class AbstractEventExecution < StandardError
  attr_accessor :event

  def initialize(event)
    @event = event
    super("The Event #{event} was not provided an execution block and is abstract.")
  end
end

event1 = Event.new(1)
event2 = Event.new(2)
event3 = Event.new(3)
t1 = event1.execute { puts "hai from event 1" }
t2 = event2.execute { puts "hai from event 2" }
t3 = event3.execute { puts "hai from event 3" }

# waiting for all threads to finish
[t1, t2, t3].map(&:wait)
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57842823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档