Last active
August 29, 2015 14:10
-
-
Save itod/b0c481176ae2bd9c0cae to your computer and use it in GitHub Desktop.
Java Confinement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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