Java and Android Concurrency
Concurrency and GUIs
[email protected]:spoto/java-and-android-concurrency.git
[email protected]:spoto/java-and-android-concurrency-examples.git
Fausto Spoto Universita di Verona, Italy - 1 / 45
Graphical Applications
Graphical User Interfaces (GUI) interact with the user through widgets
windows
buttons
labels
text fields
sliders
menus
The Swing and Android libraries implement such components throughclassical design patterns: strategy, composite, decorator . . .
For now, let us consider the simpler case of Swing programming
Fausto Spoto Universita di Verona, Italy - 2 / 45
Why Concurrency is Related to GUI Programming
GUI data structures are typically non-thread-safe
they cannot be used by more threads concurrently
a special thread takes care of their management (main thread, userinterface thread, event dispatch thread (EDT), graphical thread. . . )
long tasks cannot be executed in the user interface thread, but mustbe offlined to worker threads
when worker threads complete, they usually have to schedule eventsfor the EDT
Fausto Spoto Universita di Verona, Italy - 3 / 45
GUI Libraries Use Thread Confinement
GUI data structures are confined to the EDT:
Picture taken from http://parallel.auckland.ac.nz/ParallelIT/PT_QuickStart.html
Fausto Spoto Universita di Verona, Italy - 4 / 45
Communication between EDT and Worker Threads
The EDT can schedule tasks on a worker thread by using any techniquefor spawning concurrent executions, such as:
create and start a Thread
submit the task to an executor
The worker thread asks the EDT to run a task by calling the method
void java.awt.EventQueue.invokeLater(Runnable task)
Fausto Spoto Universita di Verona, Italy - 5 / 45
A First Example of the Use of Swing
Fausto Spoto Universita di Verona, Italy - 6 / 45
Opening a New Window in Swing
Fausto Spoto Universita di Verona, Italy - 7 / 45
A Frame Implements a Window and Contains Components
Fausto Spoto Universita di Verona, Italy - 8 / 45
A Component Knows how to Draw Itself
Fausto Spoto Universita di Verona, Italy - 9 / 45
A Second Example of the Use of Swing
Fausto Spoto Universita di Verona, Italy - 10 / 45
A Second Example of the Use of Swing
Fausto Spoto Universita di Verona, Italy - 11 / 45
A Second Example of the Use of Swing
Fausto Spoto Universita di Verona, Italy - 12 / 45
A Third Example of the Use of Swing: Library Components
Fausto Spoto Universita di Verona, Italy - 13 / 45
TextFields, Labels, Container Panels. . .
Fausto Spoto Universita di Verona, Italy - 14 / 45
Text Areas, Scroll Bar Decorators. . .
Fausto Spoto Universita di Verona, Italy - 15 / 45
Interactive Components
A listener specifies the behavior of the click on the button:
The Hollywood Principle: Don’t call me, I’ll call you
Fausto Spoto Universita di Verona, Italy - 16 / 45
Listeners as Explicit Classes (Name Pollution)
Fausto Spoto Universita di Verona, Italy - 17 / 45
Listeners as Inner Classes (Still too Long)
Fausto Spoto Universita di Verona, Italy - 18 / 45
Listeners as Anonymous Inner Classes (OK-ish)
Fausto Spoto Universita di Verona, Italy - 19 / 45
Listeners as Lambdas (only Java 8) (great!)
Fausto Spoto Universita di Verona, Italy - 20 / 45
Buttons
Fausto Spoto Universita di Verona, Italy - 21 / 45
A Serious Swing Application!
Fausto Spoto Universita di Verona, Italy - 22 / 45
Elections: First View
We will develop a system with two interfaces, for handling election charts
by clicking on a party name one can increase its votes
by clicking on the minus sign, one can remove a party
by clicking on the plus sign, one can add a new party
Fausto Spoto Universita di Verona, Italy - 23 / 45
Elections: Second View
by clicking on a party name one can increase its votes
no provision for adding/removing parties
Fausto Spoto Universita di Verona, Italy - 24 / 45
Separation of Concerns
The graphical interface should be kept separate from the logic:
for distinct versions
for desktop
for Android
for special accessibility
Data should be kept separate from the logic:
faster on desktop
more compact on mobile
kept in a database
accessible through a web interface
Fausto Spoto Universita di Verona, Italy - 25 / 45
The Model-View-Controller Design Pattern
Fausto Spoto Universita di Verona, Italy - 26 / 45
The Organization into Packages
model: data representation
view: game views
controller: data/view coordination
In order to clarify which thread can call which method, we can annotatethe latter as follows:
@UiThread: for methods that can only be executed by the EDT
@WorkerThread: for methods that cannot be executed by the EDT
Current compilers do not check such annotations, but static analyzers arestarting checking them
Fausto Spoto Universita di Verona, Italy - 27 / 45
The MVC Triple 1/2
Fausto Spoto Universita di Verona, Italy - 28 / 45
The MVC Triple 2/2
Fausto Spoto Universita di Verona, Italy - 29 / 45
The Starting Point of the Program
Fausto Spoto Universita di Verona, Italy - 30 / 45
The Model
Fausto Spoto Universita di Verona, Italy - 31 / 45
The Model: Implementation
Fausto Spoto Universita di Verona, Italy - 32 / 45
The Controller
Fausto Spoto Universita di Verona, Italy - 33 / 45
The View
Fausto Spoto Universita di Verona, Italy - 34 / 45
The View: First Implementation 1/4
Fausto Spoto Universita di Verona, Italy - 35 / 45
The View: First Implementation 2/4
Fausto Spoto Universita di Verona, Italy - 36 / 45
The View: First Implementation 3/4
Fausto Spoto Universita di Verona, Italy - 37 / 45
The View: First Implementation 4/4
Fausto Spoto Universita di Verona, Italy - 38 / 45
A Custom Component for Histograms
Fausto Spoto Universita di Verona, Italy - 39 / 45
The View: Second Implementation 1/3
Fausto Spoto Universita di Verona, Italy - 40 / 45
The View: Second Implementation 2/3
Fausto Spoto Universita di Verona, Italy - 41 / 45
The View: Second Implementation 3/3
Fausto Spoto Universita di Verona, Italy - 42 / 45
Exercise 1: Generate Votes through an Election Server
Write and publish a servlet with a single method:
sendvotes?howmany=XXX&parties=P1,P2,...,Pn
that sends back howmany random votes for the given, comma-separatedparty names. Votes are reported back to the client as a sequence of howmanyparty names from P1,P2,...,Pn, each standing for a vote for that party:
flower party
great party
PCDD
great party
new party
flower party
great party
...
Fausto Spoto Universita di Verona, Italy - 43 / 45
Exercise 2: Import Votes from an Election Server
Modify both views for the election example, by adding a button import votesfrom server that accesses the previous servlet and registers the random votesgenerated by the server. This will modify the model and update the views
You Need a Worker Thread
The connection to the servlet cannot be executed in the EDT, since it mightbe slow and is potentially blocking. Spawn a thread instead and report theresults back to the EDT at the end
Is it wise to generate an EDT event for each vote that gets read and reg-istered to the model? Is it possible instead to batch all updates into arestricted number of EDT events?
Fausto Spoto Universita di Verona, Italy - 44 / 45
Exercise 3: Third Implementation of the Elections View
Implement a third version of the view, where the names of the parties arereported in the top part of the frame, in distinct colors, and below is reporteda pie chart, with colors corresponding to those of the parties. No provisionfor adding or removing parties
See a pie chart Swing component at:http://blue-walrus.com/2012/09/simple-pie-chart-in-java-swing
Fausto Spoto Universita di Verona, Italy - 45 / 45