All Projects → SilenceDut → Taskscheduler

SilenceDut / Taskscheduler

A concise,practical async library for Android project,already was used in million devices

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Taskscheduler

Lightio
LightIO is a userland implemented green thread library for ruby
Stars: ✭ 165 (-57.14%)
Mutual labels:  async, thread
Nevercrash
🌍 全局捕获Crash。信NeverCrash,永不Crash。
Stars: ✭ 170 (-55.84%)
Mutual labels:  thread, handler
Avoid Memory Leak Android
🔥 Examples of memory leaks and common patterns that cause them in Android development and how to fix/avoid them
Stars: ✭ 140 (-63.64%)
Mutual labels:  thread, handler
Android Startup
🔥The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.
Stars: ✭ 635 (+64.94%)
Mutual labels:  async, thread
Transmittable Thread Local
📌 TransmittableThreadLocal (TTL), the missing Java™ std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even using thread pooling components.
Stars: ✭ 4,678 (+1115.06%)
Mutual labels:  async, thread
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+79.22%)
Mutual labels:  async, thread
Context Switch
Comparison of Rust async and Linux thread context switch time.
Stars: ✭ 483 (+25.45%)
Mutual labels:  async, thread
Taskmanager
A simple、 light(only two file)、fast 、powerful 、easy to use 、easy to extend 、 Android Library To Manager your AsyncTask/Thread/CallBack Jobqueue ! 一个超级简单,易用,轻量级,快速的异步任务管理器,类似于AsyncTask,但是比AsyncTask更好用,更易控制,从此不再写Thread ! ^_^
Stars: ✭ 25 (-93.51%)
Mutual labels:  async, thread
Gear Lib
Gear-Lib, C library for IOT Embedded Multimedia and Network
Stars: ✭ 2,381 (+518.44%)
Mutual labels:  async, thread
Predis Async
Asynchronous PHP client library for Redis built on top of ReactPHP
Stars: ✭ 354 (-8.05%)
Mutual labels:  async
Aiodnsbrute
Python 3.5+ DNS asynchronous brute force utility
Stars: ✭ 370 (-3.9%)
Mutual labels:  async
Serve Handler
The foundation of `serve`
Stars: ✭ 349 (-9.35%)
Mutual labels:  handler
Alloy Worker
面向事务的高可用 Web Worker 通信框架
Stars: ✭ 349 (-9.35%)
Mutual labels:  thread
Lahja
Lahja is a generic multi process event bus implementation written in Python 3.6+ to enable lightweight inter-process communication, based on non-blocking asyncio
Stars: ✭ 374 (-2.86%)
Mutual labels:  async
Ng2 Tree
Angular tree component
Stars: ✭ 350 (-9.09%)
Mutual labels:  async
React Query
⚛️ Hooks for fetching, caching and updating asynchronous data in React
Stars: ✭ 24,427 (+6244.68%)
Mutual labels:  async
Use Package
A use-package declaration for simplifying your .emacs
Stars: ✭ 3,748 (+873.51%)
Mutual labels:  handler
Gitui
Blazing 💥 fast terminal-ui for git written in rust 🦀
Stars: ✭ 6,762 (+1656.36%)
Mutual labels:  async
Many requests
Dead easy interface for executing many HTTP requests asynchronously. Also provides helper functions for executing embarrassingly parallel async coroutines.
Stars: ✭ 384 (-0.26%)
Mutual labels:  async
Sqlx
🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, and MSSQL.
Stars: ✭ 5,039 (+1208.83%)
Mutual labels:  async

TaskScheduler

A concise,practical async library for Android project,already was used in million devices

About

Android的日常开发中,经常需要处理异步任务,系统提供的Handler和Asynctask实在不方便,一些开源的第三方库由于过于庞大和复杂,例如RxJava,虽然很方便,但RxJava在团队中不容易推广,所以实现了一个简洁、方便、实用的异步库。

该异步库已经用于多个日活过百万线上项目中,暂时没发现什么问题,由于线上项目对崩溃率的要求比较严格,可以规避了执行异步任务或UI操作等带来的崩溃。这个库因为单独抽离出来,实际线上会有些差别,但核心思想是一样的,相对原来的更简洁一些。

这个库用起来会很简单,只提供少量的接口,但功能会很强大,其实目的也就是这,就是利用已经相对完善的jdk或者sdk提供的类进行一定的封装,让异步任务更简洁,没必要提供很多看起来功能很全但实际中基本上用不到的接口,达到一看到接口基本就能看懂该怎么用,避免臃肿。

绑定生命周期

支持当通过Handler的方式在Activity、Fragment中执行或延迟执行匿名内部类的runnable时,当onDestroy时,runnable自动被移除,极大简化使用,避免内存泄漏

在Activity里或者Fragment里使用时,在onDestory不需要显示的移除匿名内部类(注意support版本要大于27,官方建议一直用最新的compileSdkVersion和supportVersion)

TaskScheduler.runOnUIThread(this,new Runnable() {
    @Override
    public void run() {
        
    }
},5000);

可指定在特定生命周期移除未执行任务,如onStop

TaskScheduler.runOnUIThread(this, Lifecycle.Event.ON_STOP, new Runnable() {
    @Override
    public void run() {
        Log.i("LifeFragment","runTask with life on Stop");
    }
},5000);

外部传入任意Handler

TaskScheduler.runLifecycleRunnable(this,anyHandler,,new Runnable() {
    @Override
    public void run() {
        
    }
},5000);


Using

简单的不需要回调的异步任务

TaskScheduler.execute(new Runnable() {
    @Override
    public void run() {
    // DO BACKGROUND WORK
    }
});

异步需要回调的任务

Task task = new Task<String>() {

    @Override
    public String doInBackground()  {
        return "background task";
    }

    @Override
    public void onSuccess(String result) {
       //回调到主线程
    }

    @Override
    public void onFail(Throwable throwable) {
        super.onFail(throwable);
        //doInBackground 里发生错误时回调
    }

    @Override
    public void onCancel() {
        super.onCancel();
        //任务被取消时回调
    }
});

doInBackground 和 onSuccess(Object result)时默认必须实现的接口,其他实现都是可选的。除了doInBackground在异步线程中执行,其他的都会回调到主线程执行。

执行任务

TaskScheduler.execute(task);

取消任务

将会回调到onCancel(),没法真正取消正在执行的任务,只是结果不在onSuccess里回调, 不一定能让任务停止,和AsyncTask同样道理,可参考之前写的一篇博客为什么AsyncTask的cancel不能真正的让线程终止运行

TaskScheduler.cancel(task);

超时任务

如果任务超时,将会回调到onCancel()

TaskScheduler.executeTimeOutTask(timeOutMillis,task);

周期性任务

如果任务超时,将会回调到onCancel()

主线程,Io线程可选
TaskScheduler.scheduleUITask( SchedulerTask task);

取消任务
TaskScheduler.stopScheduleTask(SchedulerTask task) 

其他的一些常用方法

/**
*获取回调到handlerName线程的handler.一般用于在一个后台线程执行同一种任务,避免线程安全问题。如数据库,文件操作
*/
Handler provideHandler(String handlerName)

 /**
 * 提供一个公用的异步handler
 */
Handler ioHandler()

/**
*常用的handler的操作
*/
runOnUIThread(@NonNull Runnable runnable)

runOnUIThread(Runnable runnable,long delayed)

removeUICallback(@NonNull Runnable runnable)


Add to library

Step1.Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step2. Add the dependency:

dependencies {
	compile 'com.github.silencedut:taskscheduler:latest'
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].