1
- require 'thread'
2
-
3
1
require_relative 'executor'
4
- require 'concurrent/atomic/event'
5
2
6
3
module Concurrent
7
4
@@ -15,38 +12,9 @@ class RubySingleThreadExecutor
15
12
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
16
13
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
17
14
def initialize ( opts = { } )
18
- @mutex = Mutex . new
19
15
@queue = Queue . new
20
16
@thread = nil
21
- @stop = Event . new
22
- @stopped = Event . new
23
- end
24
-
25
- # Is the thread pool running?
26
- #
27
- # @return [Boolean] `true` when running, `false` when shutting down or shutdown
28
- def running?
29
- ! @stop . set?
30
- end
31
-
32
- # Is the thread pool shutdown?
33
- #
34
- # @return [Boolean] `true` when shutdown, `false` when shutting down or running
35
- def shutdown?
36
- @stopped . set?
37
- end
38
-
39
- # Block until thread pool shutdown is complete or until `timeout` seconds have
40
- # passed.
41
- #
42
- # @note Does not initiate shutdown or termination. Either `shutdown` or `kill`
43
- # must be called before this method (or on another thread).
44
- #
45
- # @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
46
- #
47
- # @return [Boolean] `true` if shutdown complete or false on `timeout`
48
- def wait_for_termination ( timeout )
49
- @stopped . wait ( timeout . to_i )
17
+ init_executor
50
18
end
51
19
52
20
# Submit a task to the thread pool for asynchronous processing.
@@ -61,7 +29,7 @@ def wait_for_termination(timeout)
61
29
# @raise [ArgumentError] if no task is given
62
30
def post ( *args , &task )
63
31
raise ArgumentError . new ( 'no block given' ) unless block_given?
64
- @ mutex. synchronize do
32
+ mutex . synchronize do
65
33
break false unless running?
66
34
supervise
67
35
@queue << [ args , task ]
@@ -73,11 +41,11 @@ def post(*args, &task)
73
41
# but no new tasks will be accepted. Has no additional effect if the
74
42
# thread pool is not running.
75
43
def shutdown
76
- @ mutex. synchronize do
44
+ mutex . synchronize do
77
45
return unless running?
78
- @stop . set
46
+ stop_event . set
79
47
@queue << :stop
80
- @stopped . set unless alive?
48
+ stopped_event . set unless alive?
81
49
end
82
50
end
83
51
@@ -86,12 +54,12 @@ def shutdown
86
54
# will be accepted. Has no additional effect if the thread pool is
87
55
# not running.
88
56
def kill
89
- @ mutex. synchronize do
57
+ mutex . synchronize do
90
58
return if shutdown?
91
- @stop . set
59
+ stop_event . set
92
60
@queue . clear
93
61
@thread . kill if alive?
94
- @stopped . set
62
+ stopped_event . set unless alive?
95
63
end
96
64
end
97
65
@@ -122,7 +90,7 @@ def work
122
90
# let it fail
123
91
end
124
92
end
125
- @stopped . set
93
+ stopped_event . set
126
94
end
127
95
end
128
96
end
0 commit comments