AndroidAnnotations——WorkingWithThreads线程操作

WorkingWithThreads

Since AndroidAnnotations 1.0

Get rid of AsyncTasks!!摆脱AsyncTasks!!

@Background

The @Background annotation indicates that a method will run in a thread other than the ui thread.@Background 注解标识一个方法将在另一个线程中,而不是ui thread(主线程)中运行。

Usage example:用例:

void myMethod() {
    someBackgroundWork("hello", 42);}

@Backgroundvoid someBackgroundWork(String aParam, long anotherParam) {
    [...]
}
The method is executed on a separate thread, but this doesn't necessarily mean that a new thread will be started, because we use a shared cached  thread pool executor (which can be replaced) to prevent creating too much threads.
这个方法将在独立的线程中执行,但是这不一定意味着一个新线程的开始,因为我们使用了一个共享缓存   thread pool executor  (可以被替代) 来阻止产生太多的线程。

This also means that two  @Background methods may run in parallel
这也意味着两个   @Background   方法可能异步运行

Id

Since AndroidAnnotations 3.0

If you want to cancel a background task, you can use the id field. Every task could then be cancelled by BackgroundExecutor.cancelAll("id");:假如你想要取消一个后台任务,你可以使用 id字段。然后每个任务都可以使用 BackgroundExecutor.cancelAll("id");进行取消:

void myMethod() {
    someCancellableBackground("hello", 42);
    [...]
    boolean mayInterruptIfRunning = true;
    BackgroundExecutor.cancelAll("cancellable_task", mayInterruptIfRunning);}

@Background(id="cancellable_task")
void someCancellableBackground(String aParam, long anotherParam) {
    [...]
}

Serial

Since AndroidAnnotations 3.0

By default, @Background method are processed in parallel. If you want them to be executed sequentially, you can use the serial field. All background tasks with the same serial will be executed sequentially:默认情况下,@Background 方法是异步执行的。假如你想要它们按顺序执行,你可以使用 serial 字段。所有使用相同 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);
}

Delay

Since AndroidAnnotations 3.0

If you need to add a delay before a background method is run, you can use the delay parameter:如果你需要在一个后台方法运行前增加延迟,你可以使用 delay 参数:

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

@UiThread

The @UiThread annotation indicates that a method will run in the ui thread.@UiThread 注解标识一个方法将在ui thread(主线程)中运行。

Usage example:用例:

void myMethod() {
    doInUiThread("hello", 42);}

@UiThread
void doInUiThread(String aParam, long anotherParam) {
    [...]
}

No more AsyncTask<Param, Progress, Result>!!不再有 AsyncTask<Param, Progress, Result>!!

Delay

If you need to add a delay before a method is run on the UI Thread, you can use the delayparameter:如果你需要在一个UI线程运行前增加延迟,你可以使用 delay 参数:

@UiThread(delay=2000)
void doInUiThreadAfterTwoSeconds() {
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值