1
1
/**
2
2
* Copyright 2014 Netflix, Inc.
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
7
+ *
8
8
* http://www.apache.org/licenses/LICENSE-2.0
9
- *
9
+ *
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
*/
16
16
package rx .schedulers ;
17
17
18
- import java .util .PriorityQueue ;
18
+ import java .util .concurrent . PriorityBlockingQueue ;
19
19
import java .util .concurrent .TimeUnit ;
20
20
import java .util .concurrent .atomic .AtomicInteger ;
21
21
import java .util .concurrent .atomic .AtomicIntegerFieldUpdater ;
@@ -45,12 +45,11 @@ public Worker createWorker() {
45
45
/* package accessible for unit tests */ TrampolineScheduler () {
46
46
}
47
47
48
- volatile int counter ;
49
- static final AtomicIntegerFieldUpdater <TrampolineScheduler > COUNTER_UPDATER = AtomicIntegerFieldUpdater .newUpdater (TrampolineScheduler .class , "counter" );
48
+ private static class InnerCurrentThreadScheduler extends Scheduler .Worker implements Subscription {
50
49
51
- private class InnerCurrentThreadScheduler extends Scheduler . Worker implements Subscription {
52
-
53
- final PriorityQueue <TimedAction > queue = new PriorityQueue <TimedAction >();
50
+ private static final AtomicIntegerFieldUpdater COUNTER_UPDATER = AtomicIntegerFieldUpdater . newUpdater ( InnerCurrentThreadScheduler . class , "counter" );
51
+ volatile int counter ;
52
+ private final PriorityBlockingQueue <TimedAction > queue = new PriorityBlockingQueue <TimedAction >();
54
53
private final BooleanSubscription innerSubscription = new BooleanSubscription ();
55
54
private final AtomicInteger wip = new AtomicInteger ();
56
55
@@ -70,13 +69,12 @@ private Subscription enqueue(Action0 action, long execTime) {
70
69
if (innerSubscription .isUnsubscribed ()) {
71
70
return Subscriptions .unsubscribed ();
72
71
}
73
- final TimedAction timedAction = new TimedAction (action , execTime , COUNTER_UPDATER .incrementAndGet (TrampolineScheduler . this ));
72
+ final TimedAction timedAction = new TimedAction (action , execTime , COUNTER_UPDATER .incrementAndGet (this ));
74
73
queue .add (timedAction );
75
74
76
75
if (wip .getAndIncrement () == 0 ) {
77
76
do {
78
- TimedAction polled = queue .poll ();
79
- // check for null as it could have been unsubscribed and removed
77
+ final TimedAction polled = queue .poll ();
80
78
if (polled != null ) {
81
79
polled .action .call ();
82
80
}
@@ -88,10 +86,7 @@ private Subscription enqueue(Action0 action, long execTime) {
88
86
89
87
@ Override
90
88
public void call () {
91
- PriorityQueue <TimedAction > _q = queue ;
92
- if (_q != null ) {
93
- _q .remove (timedAction );
94
- }
89
+ queue .remove (timedAction );
95
90
}
96
91
97
92
});
@@ -130,7 +125,7 @@ public int compareTo(TimedAction that) {
130
125
return result ;
131
126
}
132
127
}
133
-
128
+
134
129
// because I can't use Integer.compare from Java 7
135
130
private static int compare (int x , int y ) {
136
131
return (x < y ) ? -1 : ((x == y ) ? 0 : 1 );
0 commit comments