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() {
}
posted on 2013-06-22 15:57  lee0oo0  阅读(909)  评论(1编辑  收藏  举报