10
10
import com .lmax .disruptor .RingBuffer ;
11
11
import com .lmax .disruptor .Sequence ;
12
12
import com .lmax .disruptor .SequenceBarrier ;
13
+ import com .lmax .disruptor .TimeoutException ;
13
14
import com .lmax .disruptor .WaitStrategy ;
14
15
import com .lmax .disruptor .dsl .Disruptor ;
15
16
import com .lmax .disruptor .dsl .ExceptionHandlerWrapper ;
16
17
import com .lmax .disruptor .dsl .ProducerType ;
17
18
import java .util .concurrent .Executor ;
19
+ import java .util .concurrent .ExecutorService ;
18
20
import java .util .concurrent .ThreadFactory ;
21
+ import java .util .concurrent .TimeUnit ;
19
22
import java .util .concurrent .atomic .AtomicBoolean ;
20
23
21
24
public class Disruptor2 <T > extends Disruptor <T > {
@@ -30,9 +33,39 @@ public Disruptor2(EventFactory eventFactory, int ringBufferSize, ThreadFactory t
30
33
executor = new BasicExecutor (threadFactory );
31
34
}
32
35
33
- public Disruptor2 (EventFactory eventFactory , int ringBufferSize , ThreadFactory threadFactory , ProducerType producerType , WaitStrategy waitStrategy ) {
36
+ public Disruptor2 (EventFactory eventFactory , int ringBufferSize , ThreadFactory threadFactory , ProducerType producerType , WaitStrategy waitStrategy , ExecutorService executorService ) {
34
37
super (eventFactory , ringBufferSize , threadFactory , producerType , waitStrategy );
35
- executor = new BasicExecutor (threadFactory );
38
+ executor = executorService ;
39
+ }
40
+ private boolean hasBacklog ()
41
+ {
42
+ final long cursor = getRingBuffer ().getCursor ();
43
+
44
+ return consumerRepository .hasBacklog (cursor , false );
45
+ }
46
+ public void shutdown (final long timeout , final TimeUnit timeUnit ) throws TimeoutException
47
+ {
48
+ final long timeOutAt = System .nanoTime () + timeUnit .toNanos (timeout );
49
+ while (hasBacklog ())
50
+ {
51
+ if (timeout >= 0 && System .nanoTime () > timeOutAt )
52
+ {
53
+ throw TimeoutException .INSTANCE ;
54
+ }
55
+ // Busy spin
56
+ }
57
+ halt ();
58
+ }
59
+
60
+ /**
61
+ * Calls {@link com.lmax.disruptor.EventProcessor#halt()} on all of the event processors created via this disruptor.
62
+ */
63
+ public void halt ()
64
+ {
65
+ for (final ConsumerInfo consumerInfo : consumerRepository )
66
+ {
67
+ consumerInfo .halt ();
68
+ }
36
69
}
37
70
38
71
/**
@@ -53,10 +86,9 @@ public RingBuffer<T> start() {
53
86
54
87
return getRingBuffer ();
55
88
}
56
- private void checkOnlyStartedOnce ()
57
- {
58
- if (!started .compareAndSet (false , true ))
59
- {
89
+
90
+ private void checkOnlyStartedOnce () {
91
+ if (!started .compareAndSet (false , true )) {
60
92
throw new IllegalStateException ("Disruptor.start() must only be called once." );
61
93
}
62
94
}
0 commit comments