这个注解表明,这个方法将运行现UI线程以外的线程中
这个方法是在单独的线程上执行,但这并不一定意味着开启一个新的线程,因为会使用共享缓存线程池执行器,防止创建太多的线程。
@EActivity(R.layout.activity_test) public class TestActivity extends Activity { @UiThread void Toast(String text, int time){ Toast.makeText(this, text, time).show(); } @Background public void testBackgroundThread(String res) { Toast("show String " + res, Toast.LENGTH_LONG); } @AfterViews void afterView(){ testBackgroundThread("hello"); } }
如果你想取消后台任务,你可以使用ID字段进行取消。每个任务你都可以用 BackgroundExecutor.cancelAll("id");进行取消。
@EActivity(R.layout.activity_test) public class TestActivity extends Activity { @UiThread void Toast(String text, int time){ Toast.makeText(this, text, time).show(); } @Background(id="cancellable_task") public void testBackgroundThread(String res) { Toast("show String " + res, Toast.LENGTH_LONG); } @Click(R.id.bt_one) void cancelBackgroundThread(){ BackgroundExecutor.cancelAll("cancellable_task", true); } @AfterViews void afterView(){ testBackgroundThread("hello"); } }
在默认情况下,@Background是并行处理的。如果你想顺序的执行,您可以使用“serial”字段。所有的后台任务,如果具有相同的 serial,将被按顺序执行。
@EActivity(R.layout.activity_test) public class TestActivity extends Activity { @UiThread void Toast(String text, int time){ Toast.makeText(this, text, time).show(); } @Background(serial = "test") public void testBackgroundThread(String res,int index) { Toast("show String " + res + " , index = " + index, Toast.LENGTH_LONG); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @AfterViews void afterView(){ for (int i = 0; i < 10; i++) testBackgroundThread("hello",i); } }
如果你需要Background方法延迟一定时间运行,您可以使用 delay参数:
@EActivity(R.layout.activity_test) public class TestActivity extends Activity { @UiThread void Toast(String text, int time){ Toast.makeText(this, text, time).show(); } @Background(delay=2000) public void testBackgroundThread(String res,int index) { Toast("show String " + res + " , index = " + index, Toast.LENGTH_LONG); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @AfterViews void afterView(){ testBackgroundThread("hello",1); } }
- 如果在delay开始前,取消线程,线程不会执行
- 如果启动多个线程,线程名子一样的话,取消线程的话,所有相同名字的线程都会取消
- 如果串行执行时取消线程,后续的线程不会执行。
@UiThread
@UIThread 表明该方法将运行在UI线程上
代码示例
@EActivity(R.layout.activity_test) public class TestActivity extends Activity { @UiThread void Toast(String text, int time){ Toast.makeText(this, text, time).show(); } @Background(delay=2000) public void testBackgroundThread(String res,int index) { Toast("show String " + res + " , index = " + index, Toast.LENGTH_LONG); } @AfterViews void afterView(){ testBackgroundThread("hello",1); } }
如果你需要该方法延迟一定时间运行,您可以使用 delay参数:
@EActivity(R.layout.activity_test) public class TestActivity extends Activity { @UiThread(delay=2000) void Toast(String text, int time){ Toast.makeText(this, text, time).show(); } @Background(delay=2000) public void testBackgroundThread(String res,int index) { Toast("show String " + res + " , index = " + index, Toast.LENGTH_LONG); } @AfterViews void afterView(){ testBackgroundThread("hello",1); } }
如果你要优化UI Thread的调用,你需要设置 propagation = Propagation.REUSE这个参数。
@EActivity(R.layout.activity_test) public class TestActivity extends Activity { @UiThread(propagation = Propagation.REUSE) void Toast(String text, int time){ Toast.makeText(this, text, time).show(); } @Background(delay=2000) public void testBackgroundThread(String res,int index) { Toast("show String " + res + " , index = " + index, Toast.LENGTH_LONG); } @AfterViews void afterView(){ testBackgroundThread("hello",1); } }
@SupposeBackground、@SupposeUiThread
这两个注解可以保证方法是在正确的线程中调用
- @SupposeBackground 这个注解确保从后台线程调用,如果不是在后台进行调用会引发 IllegalStateException
- @SupposeUiThread 这个注解确保从UI线程调用。如果不是,那么就会抛出IllegalStateException。
示例代码
@EBean public class MyBean { @SupposeBackground void someMethodThatShouldNotBeCalledFromUiThread() { //if this method will be called from the UI-thread an exception will be thrown } @SupposeBackground(serial = {"serial1", "serial2"}) void someMethodThatShouldBeCalledFromSerial1OrSerial2() { //if this method will be called from another thread then a background thread with a //serial "serial1" or "serial2", an exception will be thrown } @SupposeUiThread void someMethodThatShouldBeCalledOnlyFromUiThread() { //if this method will be called from a background thread an exception will be thrown } }