Class: Codebot::ThreadController
- Inherits:
-
Object
- Object
- Codebot::ThreadController
- Defined in:
- lib/codebot/thread_controller.rb
Overview
This class provides a consistent interface for subclasses that manage a thread.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize ⇒ ThreadController
constructor
Creates a new thread controller.
-
#join ⇒ Thread?
Suspends execution of the calling thread until the managed thread exits.
-
#running? ⇒ Boolean
Checks whether the managed thread is currently running.
-
#start(arg = nil) ⇒ Thread?
Starts a new managed thread if no thread is currently running.
-
#start!(arg = nil) ⇒ Thread
Starts a new managed thread.
-
#stop ⇒ Thread?
Stops the managed thread if a thread is currently running.
-
#stop! ⇒ Thread
Stops the managed thread.
Constructor Details
#initialize ⇒ ThreadController
Creates a new thread controller.
8 9 10 |
# File 'lib/codebot/thread_controller.rb', line 8 def initialize @thread = nil end |
Instance Method Details
#join ⇒ Thread?
Suspends execution of the calling thread until the managed thread exits.
15 16 17 |
# File 'lib/codebot/thread_controller.rb', line 15 def join @thread.join if running? end |
#running? ⇒ Boolean
Checks whether the managed thread is currently running.
23 24 25 |
# File 'lib/codebot/thread_controller.rb', line 23 def running? !@thread.nil? && @thread.alive? end |
#start(arg = nil) ⇒ Thread?
Starts a new managed thread if no thread is currently running. The thread invokes the run method of the class that manages it.
33 34 35 36 37 38 39 40 |
# File 'lib/codebot/thread_controller.rb', line 33 def start(arg = nil) return if running? @thread = Thread.new do Thread.current.abort_on_exception = true run(arg) end end |
#start!(arg = nil) ⇒ Thread
Starts a new managed thread. The thread invokes the run method of the class that manages it.
48 49 50 |
# File 'lib/codebot/thread_controller.rb', line 48 def start!(arg = nil) raise "#{self.class.name} is already running" unless start(arg) end |
#stop ⇒ Thread?
Stops the managed thread if a thread is currently running.
56 57 58 59 60 |
# File 'lib/codebot/thread_controller.rb', line 56 def stop return unless running? @thread.exit end |
#stop! ⇒ Thread
Stops the managed thread.
66 67 68 |
# File 'lib/codebot/thread_controller.rb', line 66 def stop! raise "#{self.class.name} is already stopped" unless stop end |