11/**
22 * Copyright 2014 Netflix, Inc.
3- *
3+ *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
7- *
7+ *
88 * http://www.apache.org/licenses/LICENSE-2.0
9- *
9+ *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515 */
1616package rx .schedulers ;
1717
18- import java .util .PriorityQueue ;
18+ import java .util .concurrent . PriorityBlockingQueue ;
1919import java .util .concurrent .TimeUnit ;
2020import java .util .concurrent .atomic .AtomicInteger ;
2121import java .util .concurrent .atomic .AtomicIntegerFieldUpdater ;
@@ -45,12 +45,11 @@ public Worker createWorker() {
4545 /* package accessible for unit tests */ TrampolineScheduler () {
4646 }
4747
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 {
5049
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 >();
5453 private final BooleanSubscription innerSubscription = new BooleanSubscription ();
5554 private final AtomicInteger wip = new AtomicInteger ();
5655
@@ -70,13 +69,12 @@ private Subscription enqueue(Action0 action, long execTime) {
7069 if (innerSubscription .isUnsubscribed ()) {
7170 return Subscriptions .unsubscribed ();
7271 }
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 ));
7473 queue .add (timedAction );
7574
7675 if (wip .getAndIncrement () == 0 ) {
7776 do {
78- TimedAction polled = queue .poll ();
79- // check for null as it could have been unsubscribed and removed
77+ final TimedAction polled = queue .poll ();
8078 if (polled != null ) {
8179 polled .action .call ();
8280 }
@@ -88,10 +86,7 @@ private Subscription enqueue(Action0 action, long execTime) {
8886
8987 @ Override
9088 public void call () {
91- PriorityQueue <TimedAction > _q = queue ;
92- if (_q != null ) {
93- _q .remove (timedAction );
94- }
89+ queue .remove (timedAction );
9590 }
9691
9792 });
@@ -130,7 +125,7 @@ public int compareTo(TimedAction that) {
130125 return result ;
131126 }
132127 }
133-
128+
134129 // because I can't use Integer.compare from Java 7
135130 private static int compare (int x , int y ) {
136131 return (x < y ) ? -1 : ((x == y ) ? 0 : 1 );
0 commit comments