Skip to content

Instantly share code, notes, and snippets.

@itod
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save itod/b0c481176ae2bd9c0cae to your computer and use it in GitHub Desktop.

Select an option

Save itod/b0c481176ae2bd9c0cae to your computer and use it in GitHub Desktop.
Java Confinement
// MyClass.property is confined. may only be get or set on the main thread.
// Wo the `property` member is not thread-safe, but the code
// overall *is* thread safe if you confine access to `property` to the main thread only
public class MyClass {
private Object property;
public void action() {
assert isMainThread();
doInBackground({
assert !isMainThread();
Object result = doWork();
doOnMainThread({
assert isMainThread();
this.property = result;
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment