Skip to content

Thread Pool. Task/Thread switching, scheduling, without RxJava2; 线程池、线程切换、任务调度

License

Notifications You must be signed in to change notification settings

Dsiner/TaskScheduler

Repository files navigation

TaskScheduler for Android

License API Download

TaskScheduler is a library for composing asynchronous and event-based programs by using observable sequences.

Set up

Maven:

<dependency>
  <groupId>com.dsiner.lib</groupId>
  <artifactId>taskscheduler</artifactId>
  <version>1.0.2</version>
</dependency>

or Gradle:

compile 'com.dsiner.lib:taskscheduler:1.0.2'

Getting started

Execute sync task in main thread

        TaskScheduler.executeMain(new Runnable() {
            @Override
            public void run() {
                ...do something in main thread
            }
        });

Execute async task in cached thread pool

        TaskScheduler.executeTask(new Runnable() {
            @Override
            public void run() {
                ...do something in asynchronous thread
            }
        });

Execute async task in single thread pool

        TaskScheduler.executeSingle(new Runnable() {
            @Override
            public void run() {
                ...do something in asynchronous thread
            }
        });

Execute async task in a new thread

        TaskScheduler.executeNew(new Runnable() {
            @Override
            public void run() {
                ...do something in asynchronous thread
            }
        });

Create task

        TaskScheduler.create(new Task<List<String>>() {
            @Override
            public List<String> run() {
                ...do something in io thread
                return new ArrayList<>();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .map(new Function<List<String>, String>() {
                    @Override
                    public String apply(@NonNull List<String> strings) throws Exception {
                        ...do something in a new thread, such as time-consuming, map conversion, etc.
                        return "";
                    }
                })
                .observeOn(Schedulers.io())
                .map(new Function<String, Boolean>() {
                    @Override
                    public Boolean apply(@NonNull String s) throws Exception {
                        ...do something in io thread, such as time-consuming, map conversion, etc.
                        return true;
                    }
                })
                ...
                .observeOn(Schedulers.mainThread())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onNext(@NonNull Boolean result) {
                        ...do something in main thread
                    }

                    @Override
                    public void onError(Throwable e) {
                        ...do something in main thread
                    }
                });

Latest Changes

Licence

Copyright 2018 D

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.

About

Thread Pool. Task/Thread switching, scheduling, without RxJava2; 线程池、线程切换、任务调度

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages