Class: Concurrent::TimerSet
- Inherits:
-
RubyExecutorService
- Object
- Synchronization::LockableObject
- AbstractExecutorService
- RubyExecutorService
- Concurrent::TimerSet
- Defined in:
- lib/concurrent/executor/timer_set.rb
Overview
Time calculations one all platforms and languages are sensitive to changes to the system clock. To alleviate the potential problems associated with changing the system clock while an application is running, most modern operating systems provide a monotonic clock that operates independently of the system clock. A monotonic clock cannot be used to determine human-friendly clock times. A monotonic clock is used exclusively for calculating time intervals. Not all Ruby platforms provide access to an operating system monotonic clock. On these platforms a pure-Ruby monotonic clock will be used as a fallback. An operating system monotonic clock is both faster and more reliable than the pure-Ruby implementation. The pure-Ruby implementation should be fast and reliable enough for most non-realtime operations. At this time the common Ruby platforms that provide access to an operating system monotonic clock are MRI 2.1 and above and JRuby (all versions).
Executes a collection of tasks, each after a given delay. A master task monitors the set and schedules each task for execution at the appropriate time. Tasks are run on the global thread pool or on the supplied executor. Each task is represented as a ‘ScheduledTask`.
Constant Summary
Constants inherited from AbstractExecutorService
AbstractExecutorService::FALLBACK_POLICIES
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ TimerSet
constructor
Create a new set of timed tasks.
-
#kill ⇒ Object
Begin an immediate shutdown.
-
#post(delay, *args) { ... } ⇒ Concurrent::ScheduledTask, false
Post a task to be execute run after a given delay (in seconds).
Constructor Details
#initialize(opts = {}) ⇒ TimerSet
Create a new set of timed tasks.
30 31 32 |
# File 'lib/concurrent/executor/timer_set.rb', line 30 def initialize(opts = {}) super(opts) end |
Instance Method Details
#kill ⇒ Object
Begin an immediate shutdown. In-progress tasks will be allowed to complete but enqueued tasks will be dismissed and no new tasks will be accepted. Has no additional effect if the thread pool is not running.
64 65 66 |
# File 'lib/concurrent/executor/timer_set.rb', line 64 def kill shutdown end |
#post(delay, *args) { ... } ⇒ Concurrent::ScheduledTask, false
Post a task to be execute run after a given delay (in seconds). If the delay is less than 1/100th of a second the task will be immediately post to the executor.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/concurrent/executor/timer_set.rb', line 48 def post(delay, *args, &task) raise ArgumentError.new('no block given') unless block_given? return false unless running? opts = { executor: @task_executor, args: args, timer_set: self } task = ScheduledTask.execute(delay, opts, &task) # may raise exception task.unscheduled? ? false : task end |