Skip to content

Commit 96beb64

Browse files
committed
Moved duplicate code in Java thread pools to JavaAbstractThreadPool module.
1 parent cc80dc7 commit 96beb64

File tree

3 files changed

+104
-165
lines changed

3 files changed

+104
-165
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
if defined? java.util
2+
3+
module Concurrent
4+
5+
# @!macro cached_thread_pool
6+
module JavaAbstractThreadPool
7+
8+
# Is the thread pool running?
9+
#
10+
# @return [Boolean] +true+ when running, +false+ when shutting down or shutdown
11+
def running?
12+
! (shutdown? || terminated?)
13+
end
14+
15+
# Is the thread pool shutdown?
16+
#
17+
# @return [Boolean] +true+ when shutdown, +false+ when shutting down or running
18+
def shutdown?
19+
@executor.isShutdown
20+
end
21+
22+
# Were all tasks completed before shutdown?
23+
#
24+
# @return [Boolean] +true+ if shutdown and all tasks completed else +false+
25+
def terminated?
26+
@executor.isTerminated
27+
end
28+
29+
# Block until thread pool shutdown is complete or until +timeout+ seconds have
30+
# passed.
31+
#
32+
# @note Does not initiate shutdown or termination. Either +shutdown+ or +kill+
33+
# must be called before this method (or on another thread).
34+
#
35+
# @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
36+
#
37+
# @return [Boolean] +true+ if shutdown complete or false on +timeout+
38+
def wait_for_termination(timeout)
39+
@executor.awaitTermination(timeout.to_i, java.util.concurrent.TimeUnit::SECONDS)
40+
end
41+
42+
# Submit a task to the thread pool for asynchronous processing.
43+
#
44+
# @param [Array] args zero or more arguments to be passed to the task
45+
#
46+
# @yield the asynchronous task to perform
47+
#
48+
# @return [Boolean] +true+ if the task is queued, +false+ if the thread pool
49+
# is not running
50+
#
51+
# @raise [ArgumentError] if no task is given
52+
def post(*args)
53+
raise ArgumentError.new('no block given') unless block_given?
54+
@executor.submit{ yield(*args) }
55+
return true
56+
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
57+
return false
58+
end
59+
60+
# Submit a task to the thread pool for asynchronous processing.
61+
#
62+
# @param [Proc] task the asynchronous task to perform
63+
#
64+
# @return [self] returns itself
65+
def <<(task)
66+
@executor.submit(&task)
67+
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
68+
# do nothing
69+
ensure
70+
return self
71+
end
72+
73+
# Begin an orderly shutdown. Tasks already in the queue will be executed,
74+
# but no new tasks will be accepted. Has no additional effect if the
75+
# thread pool is not running.
76+
def shutdown
77+
@executor.shutdown
78+
return nil
79+
end
80+
81+
# Begin an immediate shutdown. In-progress tasks will be allowed to
82+
# complete but enqueued tasks will be dismissed and no new tasks
83+
# will be accepted. Has no additional effect if the thread pool is
84+
# not running.
85+
def kill
86+
@executor.shutdownNow
87+
return nil
88+
end
89+
90+
# The number of threads currently in the pool.
91+
#
92+
# @return [Integer] a non-zero value when the pool is running,
93+
# zero when the pool is shutdown
94+
def length
95+
running? ? 1 : 0
96+
end
97+
alias_method :size, :length
98+
end
99+
end
100+
end

lib/concurrent/java_cached_thread_pool.rb

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
if defined? java.util
22

3-
require 'concurrent/utilities'
3+
require 'concurrent/java_abstract_thread_pool'
44

55
module Concurrent
66

77
# @!macro cached_thread_pool
88
class JavaCachedThreadPool
9+
include JavaAbstractThreadPool
910

1011
# Create a new thread pool.
1112
#
@@ -14,88 +15,6 @@ def initialize(opts = {})
1415
@executor = java.util.concurrent.Executors.newCachedThreadPool
1516
end
1617

17-
# Is the thread pool running?
18-
#
19-
# @return [Boolean] +true+ when running, +false+ when shutting down or shutdown
20-
def running?
21-
! (shutdown? || terminated?)
22-
end
23-
24-
# Is the thread pool shutdown?
25-
#
26-
# @return [Boolean] +true+ when shutdown, +false+ when shutting down or running
27-
def shutdown?
28-
@executor.isShutdown
29-
end
30-
31-
# Were all tasks completed before shutdown?
32-
#
33-
# @return [Boolean] +true+ if shutdown and all tasks completed else +false+
34-
def terminated?
35-
@executor.isTerminated
36-
end
37-
38-
# Block until thread pool shutdown is complete or until +timeout+ seconds have
39-
# passed.
40-
#
41-
# @note Does not initiate shutdown or termination. Either +shutdown+ or +kill+
42-
# must be called before this method (or on another thread).
43-
#
44-
# @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
45-
#
46-
# @return [Boolean] +true+ if shutdown complete or false on +timeout+
47-
def wait_for_termination(timeout)
48-
@executor.awaitTermination(timeout.to_i, java.util.concurrent.TimeUnit::SECONDS)
49-
end
50-
51-
# Submit a task to the thread pool for asynchronous processing.
52-
#
53-
# @param [Array] args zero or more arguments to be passed to the task
54-
#
55-
# @yield the asynchronous task to perform
56-
#
57-
# @return [Boolean] +true+ if the task is queued, +false+ if the thread pool
58-
# is not running
59-
#
60-
# @raise [ArgumentError] if no task is given
61-
def post(*args)
62-
raise ArgumentError.new('no block given') unless block_given?
63-
@executor.submit{ yield(*args) }
64-
return true
65-
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
66-
return false
67-
end
68-
69-
# Submit a task to the thread pool for asynchronous processing.
70-
#
71-
# @param [Proc] task the asynchronous task to perform
72-
#
73-
# @return [self] returns itself
74-
def <<(task)
75-
@executor.submit(&task)
76-
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
77-
# do nothing
78-
ensure
79-
return self
80-
end
81-
82-
# Begin an orderly shutdown. Tasks already in the queue will be executed,
83-
# but no new tasks will be accepted. Has no additional effect if the
84-
# thread pool is not running.
85-
def shutdown
86-
@executor.shutdown
87-
return nil
88-
end
89-
90-
# Begin an immediate shutdown. In-progress tasks will be allowed to
91-
# complete but enqueued tasks will be dismissed and no new tasks
92-
# will be accepted. Has no additional effect if the thread pool is
93-
# not running.
94-
def kill
95-
@executor.shutdownNow
96-
return nil
97-
end
98-
9918
# The number of threads currently in the pool.
10019
#
10120
# @return [Integer] a non-zero value when the pool is running,

lib/concurrent/java_fixed_thread_pool.rb

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
if defined? java.util
22

3+
require 'concurrent/java_abstract_thread_pool'
34
require 'concurrent/utilities'
45

56
module Concurrent
67

78
# @!macro fixed_thread_pool
89
class JavaFixedThreadPool
10+
include JavaAbstractThreadPool
911

1012
# Create a new thread pool.
1113
#
@@ -17,88 +19,6 @@ def initialize(num_threads = Concurrent::processor_count)
1719
@executor = java.util.concurrent.Executors.newFixedThreadPool(@num_threads)
1820
end
1921

20-
# Is the thread pool running?
21-
#
22-
# @return [Boolean] +true+ when running, +false+ when shutting down or shutdown
23-
def running?
24-
! (shutdown? || terminated?)
25-
end
26-
27-
# Is the thread pool shutdown?
28-
#
29-
# @return [Boolean] +true+ when shutdown, +false+ when shutting down or running
30-
def shutdown?
31-
@executor.isShutdown
32-
end
33-
34-
# Were all tasks completed before shutdown?
35-
#
36-
# @return [Boolean] +true+ if shutdown and all tasks completed else +false+
37-
def terminated?
38-
@executor.isTerminated
39-
end
40-
41-
# Block until thread pool shutdown is complete or until +timeout+ seconds have
42-
# passed.
43-
#
44-
# @note Does not initiate shutdown or termination. Either +shutdown+ or +kill+
45-
# must be called before this method (or on another thread).
46-
#
47-
# @param [Integer] timeout the maximum number of seconds to wait for shutdown to complete
48-
#
49-
# @return [Boolean] +true+ if shutdown complete or false on +timeout+
50-
def wait_for_termination(timeout)
51-
@executor.awaitTermination(timeout.to_i, java.util.concurrent.TimeUnit::SECONDS)
52-
end
53-
54-
# Submit a task to the thread pool for asynchronous processing.
55-
#
56-
# @param [Array] args zero or more arguments to be passed to the task
57-
#
58-
# @yield the asynchronous task to perform
59-
#
60-
# @return [Boolean] +true+ if the task is queued, +false+ if the thread pool
61-
# is not running
62-
#
63-
# @raise [ArgumentError] if no task is given
64-
def post(*args)
65-
raise ArgumentError.new('no block given') unless block_given?
66-
@executor.submit{ yield(*args) }
67-
return true
68-
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
69-
return false
70-
end
71-
72-
# Submit a task to the thread pool for asynchronous processing.
73-
#
74-
# @param [Proc] task the asynchronous task to perform
75-
#
76-
# @return [self] returns itself
77-
def <<(task)
78-
@executor.submit(&task)
79-
rescue Java::JavaUtilConcurrent::RejectedExecutionException => ex
80-
# do nothing
81-
ensure
82-
return self
83-
end
84-
85-
# Begin an orderly shutdown. Tasks already in the queue will be executed,
86-
# but no new tasks will be accepted. Has no additional effect if the
87-
# thread pool is not running.
88-
def shutdown
89-
@executor.shutdown
90-
return nil
91-
end
92-
93-
# Begin an immediate shutdown. In-progress tasks will be allowed to
94-
# complete but enqueued tasks will be dismissed and no new tasks
95-
# will be accepted. Has no additional effect if the thread pool is
96-
# not running.
97-
def kill
98-
@executor.shutdownNow
99-
return nil
100-
end
101-
10222
# The number of threads currently in the pool.
10323
#
10424
# @return [Integer] a non-zero value when the pool is running,

0 commit comments

Comments
 (0)