com.gh.bmd.jrt.android.runner.MainRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jroutine-android Show documentation
Show all versions of jroutine-android Show documentation
Parallel programming on the go
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gh.bmd.jrt.android.runner;
import android.os.Looper;
import com.gh.bmd.jrt.runner.Execution;
import com.gh.bmd.jrt.runner.Runner;
import com.gh.bmd.jrt.runner.Runners;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
/**
* Implementation of a runner employing the main UI thread looper.
* Note that, when the invocation runs in the main thread, the executions with a delay of 0 will be
* performed synchronously, while the ones with a positive delay will be posted on the UI thread.
*
* Created by davide on 12/17/14.
*/
public class MainRunner extends LooperRunner {
private static final Runner sSameThreadRunner = new Runner() {
private final Runner mMain = new LooperRunner(Looper.getMainLooper(), null);
private final Runner mQueued = Runners.queuedRunner();
public void run(@Nonnull final Execution execution, final long delay,
@Nonnull final TimeUnit timeUnit) {
if (delay == 0) {
mQueued.run(execution, delay, timeUnit);
} else {
mMain.run(execution, delay, timeUnit);
}
}
};
/**
* Constructor.
*/
public MainRunner() {
super(Looper.getMainLooper(), sSameThreadRunner);
}
}