Android's Java and Oracle's Java are slightly different. Is it possible to use the following actors or coroutines
also for Android in order to avoid to use threads and share more code between Android's Java and Oracle's Java? Are there other frameworks available for both Java versions.
Thank you in advance.
Threading in AndroidIn Android, you can categorize all threading components into two basic categories: Threads that are attached to an activity/fragment: These threads are tied to the lifecycle of the activity/fragment and are terminated as soon as the activity/fragment is destroyed.
Developers multithread Android applications in order to improve their performance and usability. By spinning off processor- or resource-intensive tasks into their own threads, the rest of the program can continue to operate while these processor intensive tasks finish working.
Android has four basic types of threads. You'll see other documentation talk about even more, but we're going to focus on Thread , Handler , AsyncTask , and something called HandlerThread .
So, it's not a “magical” technology, that is better than threads, but just a different concept of concurrency used in your applications. Unlike threads, coroutines are not bound to any particular thread. A coroutine can start executing in one thread, suspend execution, and resume on a different thread.
You should try actors from Java version of Akka: http://doc.akka.io/docs/akka/snapshot/java/untyped-actors.html
It allows easy growing of actor topology using best practices (no direct access to actors by Java reference, limiting failures to zones, limiting overload using scheduler zones etc.) - all of them are described in free copy of "Scala in Depth" book: http://typesafe.com/resources/scala-in-depth
Here is example (with demo http://vimeo.com/20303656) of dynamic behavior of actors using Akka FSM on Android: https://github.com/akka/akka/blob/master/akka-samples/akka-sample-fsm/src/main/scala/DiningHakkersOnBecome.scala
Also you can try lightweight actors from Functional Java: https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/control/parallel/Actor.java
Most minimalistic version of an actor for JVM is here: https://github.com/plokhotnyuk/actors/blob/master/src/test/scala/com/github/gist/viktorklang/Actor.scala (its features described here: How to implement actor model without Akka? )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With