zoukankan      html  css  js  c++  java
  • Andorid之Annotation框架初使用(三)

    线程使用:

    @Background
    这个是使用了cached thread pool executor , 阻止开启过多的线程

    可以为@Background指定一个id,用于随时终止线程的操作(BackgroundExecutor.cancelAll())

    void myMethod() {
        someCancellableBackground("hello", 42);
        [...]
        boolean mayInterruptIfRunning = true;
        BackgroundExecutor.cancelAll("cancellable_task", mayInterruptIfRunning);
    }
    
    @Background(id="cancellable_task")
    void someCancellableBackground(String aParam, long anotherParam) {
        [...]
    }


    默认程况下,@Background是并发的,但也可以设置为穿行, 只需要设置相同的  serial  即可

    void myMethod() {
        for (int i = 0; i < 10; i++)
            someSequentialBackgroundMethod(i);
    }
    
    @Background(serial = "test")
    void someSequentialBackgroundMethod(int i) {
        SystemClock.sleep(new Random().nextInt(2000)+1000);
        Log.d("AA", "value : " + i);
    }


    设置后台线程延迟执行

    @Background(delay=2000)
    void doInBackgroundAfterTwoSeconds() {
    }


    UI线程:执行在UI线程上,这个也可以设置延迟时间

    @UiThread(delay=2000)
    void doInUiThreadAfterTwoSeconds() {
    }
  • 相关阅读:
    slf4j+log4j2的配置
    日志规约
    log4j2配置文件log4j2.xml详解(转载)
    好用的打包工具webpack
    gulp插件
    学习自动化工具gulp
    git
    nodejs学习随笔
    好用的meta标签
    小问题记录
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3149843.html
Copyright © 2011-2022 走看看