@@ -14,6 +14,7 @@ public class OperationQueue {
14
14
private long maxExecutionTime = -1 ;
15
15
private int maxConcurrentOperations = 0 ;
16
16
private ExecutorService executor = null ;
17
+ private boolean terminateExecutorAutomatically = true ;
17
18
private boolean autoFinishOperationsOnCompletion = false ;
18
19
private OperationList operationList = new OperationList ();
19
20
@@ -34,6 +35,15 @@ public OperationQueue(int maxConcurrentOperations) {
34
35
setMaxConcurrentOperationsCount (maxConcurrentOperations );
35
36
}
36
37
38
+ /**
39
+ * Initializes operation queue with a custom executor.
40
+ * @param customExecutor
41
+ */
42
+ public OperationQueue (ExecutorService customExecutor ) {
43
+ this .executor = customExecutor ;
44
+ terminateExecutorAutomatically = false ;
45
+ }
46
+
37
47
/**
38
48
* Adds the specified operation to the queue and begins it's execution provided all its dependencies are satisfied.
39
49
* @param operation
@@ -121,7 +131,7 @@ void executeOperation(Operation operation) {
121
131
synchronized void notifyOperationComplete (Operation operation ) {
122
132
operationList .removeOperation (operation );
123
133
124
- if (operationList .isEmpty ()) {
134
+ if (operationList .isEmpty () && terminateExecutorAutomatically ) {
125
135
executor .shutdownNow ();
126
136
}
127
137
}
@@ -136,4 +146,4 @@ private void trackOperation(Operation operation) {
136
146
private synchronized void initExecutor () {
137
147
this .executor = Executors .newFixedThreadPool (maxConcurrentOperations );
138
148
}
139
- }
149
+ }
0 commit comments