From 2f39f2a3fbae42906c5ed12e418f064f29a03d0d Mon Sep 17 00:00:00 2001 From: Ilia Kurtov Date: Thu, 25 May 2017 16:15:25 +0300 Subject: [PATCH] change executor to single thread and add priority for execution --- .../SequenceObservableExecutor.java | 7 ++- .../utils/ProcessPriorityThreadFactory.java | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ru/touchin/roboswag/core/utils/ProcessPriorityThreadFactory.java diff --git a/src/main/java/ru/touchin/roboswag/core/observables/SequenceObservableExecutor.java b/src/main/java/ru/touchin/roboswag/core/observables/SequenceObservableExecutor.java index b1e3874..f884e82 100644 --- a/src/main/java/ru/touchin/roboswag/core/observables/SequenceObservableExecutor.java +++ b/src/main/java/ru/touchin/roboswag/core/observables/SequenceObservableExecutor.java @@ -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 { diff --git a/src/main/java/ru/touchin/roboswag/core/utils/ProcessPriorityThreadFactory.java b/src/main/java/ru/touchin/roboswag/core/utils/ProcessPriorityThreadFactory.java new file mode 100644 index 0000000..64c826b --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/core/utils/ProcessPriorityThreadFactory.java @@ -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; + } + +} \ No newline at end of file