Netflix/RxJava — pull request #1047 by benjchristensen (2014-04-18 05:29:35+00:00)
Title: Scheduler Simplification

API redesign (simplification) as per discussion at https://github.com/Netflix/RxJava/issues/997

The `Scheduler` API in this pull request is:

```java
class Scheduler {
public abstract Inner Inner();
public int degreeOfParallelism();
public long now();

public abstract static class Inner implements Subscription {
public abstract Subscription schedule(Action0 action, long delayTime, TimeUnit unit);
public abstract Subscription schedule(Action0 action);
public final void schedulePeriodically(Action0 action, long initialDelay, long period, TimeUnit unit);
public long now();
}
}
```

While migrating to this I found a 4th use case in addition to the previously discussed 3 that required the `schedule` methods inside `Inner` to also return `Subscription` for canceling the individual tasks as opposed to the entire `Inner`.

Here are the use cases:

#### 1) Single Action

```java
final Inner is = scheduler.inner();
is.schedule(new Action0() {

@Override
public void call() {
// do work here
}
})

is.unsubscribe()
```

with lambda

```java
final Inner is = scheduler.inner);
is.schedule(() -> {
// do work here
})

is.unsubscribe()
```

#### 2) Inner Recursion

```java
final Inner is = scheduler.inner();
is.schedule(new Action0() {

@Override
public void call() {
// do work here then recursively reschedule
is.schedule(this); // this will NOT work with lambdas, only anonymous inner classes
}
})

is.unsubscribe()
```

#### 3) Outer Recursion

```java
final Inner is = scheduler.inner();

public void onNext(T t) {
is.schedule(new Action0() {

@Override
public void call() {
// do work here
}
})
}

is.unsubscribe()
```

#### 4) Outer Recursion with Task Cancellation

This is used for things like `debounce` and `throttleLast` where tasks are being scheduled and cancelled as `onNext` notifications are received.

```java
final Inner is = scheduler.inner();
SerialSubscription serial = new SerialSubscription();

public void onNext(T t) {
// schedule a new task and cancel previous if not yet executed
serial.set(is.schedule(new Action0() {

@Override
public void call() {
// do work here
}
}));
}

is.unsubscribe()
```