Code Monkey home page Code Monkey logo

handlerthreadplus's Introduction

HandlerThreadPlus

项目地址

概述

本质上是一个安卓的 HandlerThread,但是,HandlerThread 是一直运行的,HandlerThreadPlus 在没有消息处理一段时间后,线程会自动结束。

优点:

  1. 避免使用太多 HandlerThread 造成应用卡顿

使用

1. 引用

拷贝 util 包下的工具类到自己项目中

工具类

2. 创建线程和事件

Event 可以是任意一个 JavaBean,如:

public class SendMsgEvent {
  private String content;
}

Thread 继承 MHandlerThread,并制定泛型,如:

public class SendMsgThread extends MHandlerThread<SendMsgThread> {

    @Override
    protected void execute(SendMsgThread event) {
        // TODO 线程要处理的事情
    }
}

3. 注册线程

HandlerThreadPlus.register(SendMsgThread.class);

HandlerThreadPlus.register(SendMsgThread.class, 6000l);

则线程 SendMsgThread 在处理完最后一个消息时,等待 6000 毫秒没有消息进入则结束线程

修改默认线程等待时间

HandlerThreadPlus.setAllThreadSleepTime(long allThreadSleepTime)

4. 发布事件

HandlerThreadPlus.send(Object event);

注意

  • 目前 Event 和 Thread 仅支持一对一

handlerthreadplus's People

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.