change executor to single thread and add priority for execution

This commit is contained in:
Ilia Kurtov 2017-05-25 16:15:25 +03:00
parent cf7087417f
commit 2f39f2a3fb
2 changed files with 55 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import android.support.annotation.Nullable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import ru.touchin.roboswag.core.utils.ProcessPriorityThreadFactory;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
@ -25,6 +26,9 @@ public class SequenceObservableExecutor {
@NonNull
private final Scheduler sendingScheduler = Schedulers.from(Executors.newSingleThreadExecutor());
@NonNull
private final Scheduler executeScheduler = Schedulers.from(Executors.newSingleThreadExecutor(
new ProcessPriorityThreadFactory(Thread.MIN_PRIORITY)));
@NonNull
public Observable<?> execute(@NonNull final Observable<?> completable) {
@ -52,8 +56,7 @@ public class SequenceObservableExecutor {
scheduleSubscription = sendingScheduler.createWorker().schedule(() -> {
final CountDownLatch blocker = new CountDownLatch(1);
executeSubscription = completable
//TODO think how to replace it
.subscribeOn(Schedulers.newThread())
.subscribeOn(executeScheduler)
.doOnUnsubscribe(blocker::countDown)
.subscribe(Actions.empty(), subscriber::onError, subscriber::onCompleted);
try {

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov)
*
* This file is part of RoboSwag library.
*
* 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 ru.touchin.roboswag.core.utils;
import android.support.annotation.NonNull;
import java.util.concurrent.ThreadFactory;
/**
* Created by Ilia Kurtov on 25/05/2017 with a help from https://stackoverflow.com/a/21187003/4312184
* ThreadFactory that can change default thread priority. Suitable for creating Schedulers fo RxJava like this:
* final Scheduler scheduler = Schedulers.from(Executors.newSingleThreadExecutor(new ProcessPriorityThreadFactory(Thread.MIN_PRIORITY)));
*/
public final class ProcessPriorityThreadFactory implements ThreadFactory {
private final int threadPriority;
/**
* threadPriority can be in a range from {@link Thread#MIN_PRIORITY} to {@link Thread#MAX_PRIORITY}
* @param threadPriority priority for the Thread.
*/
public ProcessPriorityThreadFactory(final int threadPriority) {
this.threadPriority = threadPriority;
}
@Override
public Thread newThread(@NonNull final Runnable runnable) {
final Thread thread = new Thread(runnable);
thread.setPriority(threadPriority);
return thread;
}
}