Coder Social home page Coder Social logo

magicthread's Introduction

MagicThread

安卓端、纯注解使用的线程切换框架

how to use

一个注解搞定线程切换

example:

    @RunOnIOThread
    public void progress() {
        for (int i = 0; i <= 100; i++) {
            showProgress(i);
            SystemClock.sleep(1000);
        }
    }

    @RunOnUIThread
    private void showProgress(int progress) {
        mTvTest.setText(progress + "%");
    }

框架提供4种线程注解:

@RunOnUIThread (被注解的方法在UI线程执行)

@RunOnIOThread (被注解的方法在子线程执行,适合IO密集型耗时操作(文件读写,网络请求))

@RunOnCalcThread (被注解的方法在子线程执行,适合计算密集型耗时操作(大量消耗CPU的计算))

@RunOnBackGround (被注解的方法在后台线程执行,所有被注解的方法都在同一个线程,队列执行,不适合耗时操作)

@RunOnUIThread(5000) 和 @RunOnBackGround(5000) 增加参数,为延时执行,单位毫秒值

依赖方法:

To get a Git project into your build:

Step 1. Add the JitPack repository to your build file

1.在全局build里面添加下面github仓库地址 Add it in your root build.gradle at the end of repositories:

buildscript {
    ...
    dependencies {
	...
        classpath 'cn.leo.plugin:magic-plugin:1.0.0' //java 用这个
	classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.0' //kotlin 用这个
    }
}
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

google()和jcenter()这两个仓库一般是默认的,如果没有请加上

Step 2. Add the dependency

2.在app的build里面添加插件和依赖

apply plugin: 'cn.leo.plugin.magic' //java 用这个
apply plugin: 'android-aspectjx'  //kotlin 用这个,编译速度会慢点
...
dependencies {
	...
	implementation 'com.github.jarryleo:MagicThread:v2.3'
}

用于支持kotlin的插件用的是 aspectjx
感谢插件作者
因为编织所有二进制文件的问题导致编译速度慢的问题,请查看原作者提供的解决方案

关于子线程在activity和fragment中进行耗时操作导致的内存泄漏,本框架提供解决办法:

在耗时操作的循环体中加入以下代码:

if (Thread.currentThread().isInterrupted()) return;

如果是采用休眠的耗时操作,请在捕获InterruptedException异常后跳出循环

注意:

只在注解 @RunOnIOThread 和 @RunOnCalcThread 的子线程中有效,利用了安卓新特性Lifecycle

其它2个注解不适合做耗时操作,不做处理

请勿把注解打在有返回值的方法上,否则会失效!!!!

magicthread's People

Contributors

jarryleo avatar

Watchers

James Cloos 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.