线程使用:

@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() {
}

相关文章:

  • 2021-11-17
  • 2022-12-23
  • 2021-06-09
  • 2021-05-09
  • 2021-11-06
  • 2022-12-23
  • 2021-10-23
  • 2021-08-22
猜你喜欢
  • 2021-12-20
  • 2021-07-26
  • 2022-02-04
  • 2021-10-03
  • 2021-12-31
  • 2021-08-25
  • 2021-05-20
相关资源
相似解决方案