15
15
*/
16
16
package rx .operators ;
17
17
18
- import java .util .concurrent .ConcurrentLinkedQueue ;
18
+ import java .util .ArrayList ;
19
+ import java .util .List ;
19
20
import java .util .concurrent .atomic .AtomicLong ;
20
21
21
22
import rx .Observable .Operator ;
@@ -60,7 +61,7 @@ private static class ObserveOnSubscriber<T> extends Subscriber<T> {
60
61
final Subscriber <? super T > observer ;
61
62
private final Scheduler .Worker recursiveScheduler ;
62
63
63
- private final ConcurrentLinkedQueue < Object > queue = new ConcurrentLinkedQueue < Object > ();
64
+ private FastList queue = new FastList ();
64
65
final AtomicLong counter = new AtomicLong (0 );
65
66
66
67
public ObserveOnSubscriber (Scheduler scheduler , Subscriber <? super T > subscriber ) {
@@ -72,19 +73,25 @@ public ObserveOnSubscriber(Scheduler scheduler, Subscriber<? super T> subscriber
72
73
73
74
@ Override
74
75
public void onNext (final T t ) {
75
- queue .offer (on .next (t ));
76
+ synchronized (this ) {
77
+ queue .add (on .next (t ));
78
+ }
76
79
schedule ();
77
80
}
78
81
79
82
@ Override
80
83
public void onCompleted () {
81
- queue .offer (on .completed ());
84
+ synchronized (this ) {
85
+ queue .add (on .completed ());
86
+ }
82
87
schedule ();
83
88
}
84
89
85
90
@ Override
86
91
public void onError (final Throwable e ) {
87
- queue .offer (on .error (e ));
92
+ synchronized (this ) {
93
+ queue .add (on .error (e ));
94
+ }
88
95
schedule ();
89
96
}
90
97
@@ -103,11 +110,43 @@ public void call() {
103
110
104
111
private void pollQueue () {
105
112
do {
106
- Object v = queue .poll ();
107
- on .accept (observer , v );
108
- } while (counter .decrementAndGet () > 0 );
113
+ FastList vs ;
114
+ synchronized (this ) {
115
+ vs = queue ;
116
+ queue = new FastList ();
117
+ }
118
+ for (Object v : vs .array ) {
119
+ if (v == null ) {
120
+ break ;
121
+ }
122
+ on .accept (observer , v );
123
+ }
124
+ if (counter .addAndGet (-vs .size ) == 0 ) {
125
+ break ;
126
+ }
127
+ } while (true );
109
128
}
110
129
111
130
}
112
131
132
+ static final class FastList {
133
+ Object [] array ;
134
+ int size ;
135
+
136
+ public void add (Object o ) {
137
+ int s = size ;
138
+ Object [] a = array ;
139
+ if (a == null ) {
140
+ a = new Object [16 ];
141
+ array = a ;
142
+ } else if (s == a .length ) {
143
+ Object [] array2 = new Object [s + (s >> 2 )];
144
+ System .arraycopy (a , 0 , array2 , 0 , s );
145
+ a = array2 ;
146
+ array = a ;
147
+ }
148
+ a [s ] = o ;
149
+ size = s + 1 ;
150
+ }
151
+ }
113
152
}
0 commit comments