Android apps need to respond fast, support highly parallel execution and multi component architecture.
Learn some tricks of the trade for these problems!
as presented at www.mobileconference.it (2013 edition)
2. Who?
"Agilist
in
a
Mobile
world"
• Mobile
Java
Developer
(2007)
• Android
(2009)
• h@p://www.androidavanzato.it
(2012)
Emanuele
Di
Saverio
Senior
Design
Technologist
@
6. The S in C/S
• a
server
program
in
(connected)
network
compuKng
while (true) {
connect();
read();
parseRequest();
process();
sendResponse();
disconnect();
}
7. MPM Prefork (Apache)
• let's
fork() and
spawn
more
processes!
full
address
space
copied
around
8. MPM Worker (Apache)
• handful
processes
spawn
a
set
of
threads
context
switch
+
shared
memory
9. Multi Threaded Programming
• Memory
is
shared,
threads
spawn
faster
• Problem
of
synchronizaKon
–
solvable
only
with
low-‐level
OS
primiKves
10. Multi Threaded Programming
• Memory
is
shared,
threads
spawn
faster
• Problem
of
synchronizaKon
–
solvable
only
with
low-‐level
OS
primiKves
"…
non-‐trivial
mul/-‐threaded
programs
are
incomprehensible
to
humans.
It
is
true
that
the
programming
model
can
be
improved:
design
pa;erns,
atomicity,
improved
languages,
and
formal
methods.
However,
these
techniques
merely
chip
away
at
the
unnecessarily
enormous
non-‐determinism
of
the
threading
model.
The
model
remains
intrinsically
intractable."
Edward
A.
Lee
–
The
Problem
with
Threads
16. The Loop
while (true) {
processInputEvents();
processSysEvents();
measure();
layout();
draw();
}
input
sys
measure
layout
draw
thread
17.
18. Ok, when it is my turn?
HOLLYWOOD
PRINCIPLE
"don't
call
me,
I'll
call
you"
managed
execuKon
LISTENERS
Button a;
[...]
a.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
object
that
receives
callback
through
define
interface
class MyActivity extends Activity {
protected void onCreate(Bundle b) {
//create my UI
}
onStop() {...}
onStart(){...}
}
19. The Android Loop
All
callbacks
are
executed
in
Main
Thread!
• AcKvity
• Service
• Fragment
• animaKons
• OnClicks
• OnTouch
• OnWhatever...
20. android.os.Looper
Make
every
thread
an
event-‐based
message
queue
public void run() {
Looper.prepare();
this.handler = new Handler()
handleMessage(int what)
if (what == 0x666) //do stuff
};
Looper.loop();}
handler.sendMessage(Message.obtain(0x666));
21. 60 FPS -> 16 ms per frame
ALL YOUR CODE NEEDS TO FIT IN 16ms
22. The Loop
while (true) {
processInputEvents();
processSysEvents();
measure();
layout();
draw();
}
input
sys
measure
layout
draw
thread
16ms
23. DON'T BLOCK UI THREAD
no
network
calls
-‐
no
file
I/O
-‐
no
intensive
computaKon
24. DON'T BLOCK UI THREAD
no
network
calls
-‐
no
file
I/O
-‐
no
intensive
computaKon
USE OTHER THREADS!
25. Asynchronous Java
Java was designed with multithreaded programming in
mind (thread ,synchronized, volatile, memory
model).
Don't rely on that – probabilities you really understand
what they do are small.
java.util.concurrent.*
full of goodies to handle concurrency
26. Asynchronous Java
You don't really create Threads that
much
Executors – Thread pool implemented
for you
ExecutorService executor =
Executors.newFixedThreadPool(10);
executor.execute(new Runnable() {
run() {
//do stuff
}
});
27. Future – a computation Stock Option
Want
to
have
result
of
an
async
execuKon?
Future<String> f1 = executor.submit(
new Callable<String> {
String call() {
return fetchNetworkContent();
}
});
f1.get(); //locks current thread
30. Future – Other Approaches
List<Future> futures = new ArrayList<Future>();
Iterator<Future<?>> i = futures.iterator();
while (i.hasNext()) {
Future<?> f = i.next();
if (f.isDone()) {
doMoreWork(f.get());
i.remove();
} }
32. Callback
Gets
around
the
early
blocking
problem
by
pre-‐
declaring
the
funcKon
that
will
be
executed
public interface Callback<T> {
public void done(T result,
Exception error);
}
39. Loader & AsyncTask
They
help
in
the
field
of
gedng
the
asynchronous
task
happen
in
a
way
that
is
compaKble
with
Android
threading
model,
but
fall
short
in:
• Combining
• ManipulaKng
• Error
Handling
• Streaming
Data
Handling
44. Reactive Extensions (Rx)
Developed
by
Microsof
for
.NET
(C#)
2009
–
Erik
Meijer
-‐
to
support
distributed
cloud
applicaEons
Open
Source
port
of
the
semanKcs
to
• Rx.ObjC,
RxJava,
Rx.JS,
Scala
• JavaVM
based
version
is
developed
by
Nejlix
–
Ben
Christensen
45. Rx (Java): Concepts
Observable
EnKty
that
can
fetch
a
stream
of
events
asyncronously,
if
you
subscribe
an
observer
to
it
Observer
You
can
imperaKvely
noKfy
an
observer
about
an
event
50. So what is Rx really doing?
Rx
is
man-‐in-‐the
middle
between
method
calling
and
callback
execuKon
onNext() != onNext()
We
have
control
over
scheduling
of
methods
53. Rx (Java): Features
Observer
is
executed
afer
someone
subscribed
Since
we
are
decoupling
method
call
and
callback
execuKon,
we
can
easily
manipulate
and
combine.
57. Rx (Java): Features
Each
observer
can
be
scheduled
to
have
his
subscribe
happen
on
any
thread,
and
all
observaKon
happen
on
any
other
thread!
Observable<Image> getImage(URL url) {
AtomicBoolean found = new AtomicBoolean(false);
obs1 = //get image from in memory cache
obs2 = //get image from disc cache
obs2 = obs2
.subscribeOn(Schedulers.executor(highprioexecutor))
.observeOn(AndroidScheduler.getInstance());
obs3 = //get image from network
obs3 = obs3
.subscribeOn(Schedulers.executor(lowprioexecutor))
.observeOn(AndroidScheduler.getInstance());
return Observable.concat(obs1, obs2, obs3);
}
58. Rx (Java): Features
Connectable
version
of
Observable
can
be
subscribed
mulKple
Kmes,
doesn't
restart
but
goes
on,
can
even
replay
past
events!
Observable<Tweet> obs = createObservableThatStreamTweet();
void initObs() {
obs.replay();
obs = obs.connect();
}
Observable<Tweet> getPopularTweets() {
return obs;
}
59. Reactive the world
• We
could
wrap
all
applicaKon
in
reacKves:
click
events
could
generate
events
that
can
route
to
DB
calls.
View
could
subscribe
to
DB
observable
and...
Yes.
Maybe.
60.
61. Reactive Over Services
<<sync>>
Employee
DAO
<<sync>>
Paypal
API
<<async>>
Android
NFC
Intents
RX
Layer
UI
/
Controllers
FragmentA
FragmentA
AcKvity
Views
72. Excellent
library
by
:
OMo
Event Bus
• Also
called
Publish
/
Subscribe
in
distributed
systems
• An
agent
posts
a
message
on
an
event
(topic)
• All
objects
registered
on
that
topic,
receive
event
• Decouple
caller
by
receiver
+
synchronous
delivery
74. Otto
• Event
delivered!
• O@o
is
not
rocket
science:
• ReflecKon
to
spot
methods
that
declare
to
handle
some
object
as
event
• search
for
method
that
accept
some
event
and
deliver
the
event
• More
features...
75. Otto
• Event
delivered!
• O@o
is
not
rocket
science:
• ReflecKon
to
spot
methods
that
declare
to
handle
some
object
as
event
• search
for
method
that
accept
some
event
and
deliver
the
event
• More
features...
Remember
to
call
unregister()!
Memory
(acKvity)
leak
danger
80. BOOST JAVA OBJ-C C# JS
you
need
to
work
your
way
towards
asynchronous
expressiveness
81. SIMPLE ABSTRACTIONS
CAN TURN YOUR WORLD
Rx
is
a
simple
abstracKon
that
enables
a
truckload
of
features
Event
Bus
is
so
old,
is
so
simple,
and
works
so
well.
82. SERVER & CLIENT
scale
is
orders
of
magnitude
smaller,
different
problems,
same
techniques.