All Projects → yjfnypeu → Easythread

yjfnypeu / Easythread

一款安全、轻巧、简单的线程池管理器

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Easythread

Acho
The Hackable Log
Stars: ✭ 189 (-24.4%)
Mutual labels:  simple
Htmr
Simple and lightweight (< 2kB) HTML string to React element conversion library
Stars: ✭ 214 (-14.4%)
Mutual labels:  simple
Ataraxia
Simple and lightweight source-based multi-platform Linux distribution with musl libc.
Stars: ✭ 226 (-9.6%)
Mutual labels:  simple
Styled React Boilerplate
Minimal & Modern boilerplate for building apps with React & styled-components
Stars: ✭ 198 (-20.8%)
Mutual labels:  simple
Sublimenotebook
📝 Make Sublime Text your favorite note taking/journal application
Stars: ✭ 203 (-18.8%)
Mutual labels:  simple
Nakedtensor
Bare bone examples of machine learning in TensorFlow
Stars: ✭ 2,443 (+877.2%)
Mutual labels:  simple
Flags
⛳ Simple, extensible, header-only C++17 argument parser released into the public domain.
Stars: ✭ 187 (-25.2%)
Mutual labels:  simple
Blockchain Python
A blockchain implementation in Python
Stars: ✭ 233 (-6.8%)
Mutual labels:  simple
Vue Impression
A Vue.js 2.0 UI elements for mobile.
Stars: ✭ 205 (-18%)
Mutual labels:  simple
Sol2
Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
Stars: ✭ 2,791 (+1016.4%)
Mutual labels:  simple
Gallery shell
📷 Bash Script to generate static responsive image web galleries.
Stars: ✭ 198 (-20.8%)
Mutual labels:  simple
Mu
The μ css framework — a 1 ko css file.
Stars: ✭ 202 (-19.2%)
Mutual labels:  simple
Simple Dash
A simple, fully responsive Dashboard to forward to the services of your choice!
Stars: ✭ 222 (-11.2%)
Mutual labels:  simple
Threadly
A library of tools to assist with safe concurrent java development. Providing unique priority based thread pools, and ways to distrbute threaded work safely.
Stars: ✭ 196 (-21.6%)
Mutual labels:  thread-pool
Riot
Simple and elegant component-based UI library
Stars: ✭ 14,596 (+5738.4%)
Mutual labels:  simple
Bonsai
🌱 a tiny distro-independent package manager
Stars: ✭ 188 (-24.8%)
Mutual labels:  simple
Alfred Simple
Simple theme for Alfred
Stars: ✭ 217 (-13.2%)
Mutual labels:  simple
Vue Class Store
Universal Vue stores you write once and use anywhere
Stars: ✭ 243 (-2.8%)
Mutual labels:  simple
Lazyblorg
Blogging with Org-mode for very lazy people
Stars: ✭ 226 (-9.6%)
Mutual labels:  simple
Simplenetwork
simple TCP server / client C++ linux socket
Stars: ✭ 225 (-10%)
Mutual labels:  simple

EasyThread travis-ci

EasyThread通过对原生的线程池进行封装,可让你更方便的进行线程任务操作。

特性

  • 简单轻巧:方法数不过百,无额外次级依赖。
  • 配置灵活:可方便、灵活的对每次所启动的任务,配置线程名、线程优先级等。
  • 使用安全:当线程出现异常。能自动将catch异常信息传递给用户,避免出现crash。
  • 线程切换:自带线程切换功能:指定任务执行后,在哪个线程中进行用户通知。
  • 回调通知:当任务启动时与任务运行完毕后。有分别的生命周期作为通知。
  • 任务扩展:支持延迟任务以及异步回调任务

依赖

lastestVersion =

// 添加jitPack仓库使用
maven { url 'https://jitpack.io' }

// 添加依赖
compile "com.github.yjfnypeu:EasyThread:$lastestVersion"

基本用法

使用方式分两步走:

  • 第一步:创建EasyThread实例。每个EasyThread实例会持有一个独立的线程池提供使用。
EasyThread easyThread = EasyThread.Builder
            //提供了四种create方法,用于根据需要创建不同类型的线程池进行使用
            //比如createSingle():表示创建一个单例的线程池进行使用
            .createXXX()
            .build();
  • 第二步:使用创建的EasyThread实例进行任务执行:

EasyThread支持执行四种任务:

1. 普通Runnable任务

easyThread.execute(new Runnable(){
    @Override
    public void run() {
        // do something.
    }
});

2. 普通Callable任务

Future task = easyThread.submit(new Callback<User>(){
    @Override
    public User call() throws Exception {
        // do something
        return user;
    }
})
User result = task.get();

3. 异步回调任务

// 异步执行任务
Callable<User> callable = new Callable<User>(){
    @Override
    public User call() throws Exception {
        // do something
        return user;
    }
}

// 异步回调
AsyncCallback<User> async = new AsyncCallback<User>() {
    @Override
    public void onSuccess(User user) {
        // notify success;
    }

    @Override
    public void onFailed(Throwable t) {
        // notify failed.
    }
};

// 启动异步任务
easyThread.async(callable, async)

4. 延迟后台任务

// 在启动任务前,调用delay方法,指定延迟时间即可
easyThread.setDelay(time, unit)
    .execute(runnable);

e.g 延迟3秒启动执行任务

easyThread.setDelay(3, TimeUnit.SECONDS)
        .execute(task);

高级配置

EasyThread提供了各种的额外配置,通过这些配置可以让线程操作使用起来更加得心应手。

两种配置方式

这里我们以配置后台任务名进行说明:

1. 配置默认线程任务名(默认配置)

EasyThread.Builder.createXXX().setName(name);

2. 配置当前线程任务名(当前任务配置)

easyThread.setName(name).execute(task);

线程优先级及线程名

配置方式:

easyThread.setName(name)// 配置线程任务名
	.setPriority()// 配置线程运行优先级

任务回调通知

接口说明:

public interface Callback {
    // 线程任务启动时的通知
    void onStart (String threadName);
    // 线程任务运行时出现异常时的通知
    void onError (String threadName, Throwable t);
    // 线程任务正常执行完成时的通知
    void onCompleted (String threadName);
}

配置方式:

easyThread.setCallback(callback);

消息派发器

消息派发器用于消息回调线程切换,即指定回调任务需要运行在什么线程之上。

比如说在Android平台,很常见的就是回调时需要进行界面通知,所以这个时候就需要回调通知运行在UI线,便于操作。

配置方式:

// 派发器的实例类型为java.util.concurrent.Executor子类
easyThread.setDeliver(deliver);

在默认条件下(即未配置额外的派发器时),在Android或者Java平台,分别适配了不同的回调派发逻辑:

  • 在纯java环境下:回调方法所运行的线程与任务执行线程一致
  • 在Android环境:回调方法默认运行于主线程

推荐配置

对于APP来说。线程资源是宝贵的。为了避免创建过多额外的线程,所以建议对每个app。提供一个统一的管理器维护所有的线程池,如下所示:

public final class ThreadManager {

    private final static EasyThread io;
    private final static EasyThread cache;
    private final static EasyThread calculator;
    private final static EasyThread file;

    public static EasyThread getIO () {
        return io;
    }

    public static EasyThread getCache() {
        return cache;
    }

    public static EasyThread getCalculator() {
        return calculator;
    }

    public static EasyThread getFile() {
        return file;
    }

    static {
        io = EasyThread.Builder.createFixed(6).setName("IO").setPriority(7).setCallback(new DefaultCallback()).build();
        cache = EasyThread.Builder.createCacheable().setName("cache").setCallback(new DefaultCallback()).build();
        calculator = EasyThread.Builder.createFixed(4).setName("calculator").setPriority(Thread.MAX_PRIORITY).setCallback(new DefaultCallback()).build();
        file = EasyThread.Builder.createFixed(4).setName("file").setPriority(3).setCallback(new DefaultCallback()).build();
    }

    private static class DefaultCallback implements Callback {

        @Override
        public void onError(String threadName, Throwable t) {
            MyLog.e("Task with thread %s has occurs an error: %s", threadName, t.getMessage());
        }

        @Override
        public void onCompleted(String threadName) {
            MyLog.d("Task with thread %s completed", threadName);
        }

        @Override
        public void onStart(String threadName) {
            MyLog.d("Task with thread %s start running!", threadName);
        }
    }
}

联系作者

Email:[email protected] QQ group:108895031

License

Copyright 2015 HaoGe

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.
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].