Skip to content
pyricau edited this page Jan 10, 2012 · 8 revisions

Since AndroidAnnotations 1.0

Fork / Join for the poor Android dev

Let's say you want to split a background operation into two separate operations that run concurrently, and then do something on the UI thread when both are done.

Here is a simple way to implement this, thanks to @Background and @UiThread.

@EActivity
public class MyActivity extends Activity {

  static class Counter {
    int i;
  } 

  void someForkableWork() {
    Counter c = new Counter();
    doStuffA(c);
    doStuffB(c);
  }

  @Background
  void doStuffA(Counter c) {
    // Do some stuff
    joinWork(c);
  }

  @Background
  void doStuffB(Counter c) {
    // Do some stuff
    joinWork(c);
  }

  @UiThread
  void joinWork(Counter c) {
    if (++c.i < 2) {
      return;
    }

    // Do some stuff on the Ui thread
  }

}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Clone this wiki locally