@@ -45,18 +45,12 @@ public Worker createWorker() {
45
45
/* package accessible for unit tests */ TrampolineScheduler () {
46
46
}
47
47
48
- private static final ThreadLocal <PriorityQueue <TimedAction >> QUEUE = new ThreadLocal <PriorityQueue <TimedAction >>() {
49
- @ Override
50
- protected PriorityQueue <TimedAction > initialValue () {
51
- return new PriorityQueue <TimedAction >();
52
- }
53
- };
54
-
55
48
volatile int counter ;
56
49
static final AtomicIntegerFieldUpdater <TrampolineScheduler > COUNTER_UPDATER = AtomicIntegerFieldUpdater .newUpdater (TrampolineScheduler .class , "counter" );
57
50
58
51
private class InnerCurrentThreadScheduler extends Scheduler .Worker implements Subscription {
59
52
53
+ final PriorityQueue <TimedAction > queue = new PriorityQueue <TimedAction >();
60
54
private final BooleanSubscription innerSubscription = new BooleanSubscription ();
61
55
private final AtomicInteger wip = new AtomicInteger ();
62
56
@@ -76,13 +70,16 @@ private Subscription enqueue(Action0 action, long execTime) {
76
70
if (innerSubscription .isUnsubscribed ()) {
77
71
return Subscriptions .empty ();
78
72
}
79
- PriorityQueue <TimedAction > queue = QUEUE .get ();
80
73
final TimedAction timedAction = new TimedAction (action , execTime , COUNTER_UPDATER .incrementAndGet (TrampolineScheduler .this ));
81
74
queue .add (timedAction );
82
75
83
76
if (wip .getAndIncrement () == 0 ) {
84
77
do {
85
- queue .poll ().action .call ();
78
+ TimedAction polled = queue .poll ();
79
+ // check for null as it could have been unsubscribed and removed
80
+ if (polled != null ) {
81
+ polled .action .call ();
82
+ }
86
83
} while (wip .decrementAndGet () > 0 );
87
84
return Subscriptions .empty ();
88
85
} else {
@@ -91,7 +88,7 @@ private Subscription enqueue(Action0 action, long execTime) {
91
88
92
89
@ Override
93
90
public void call () {
94
- PriorityQueue <TimedAction > _q = QUEUE . get () ;
91
+ PriorityQueue <TimedAction > _q = queue ;
95
92
if (_q != null ) {
96
93
_q .remove (timedAction );
97
94
}
0 commit comments