Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and...

45
Java and Android Concurrency Concurrency and GUIs [email protected] [email protected]:spoto/java-and-android-concurrency.git [email protected]:spoto/java-and-android-concurrency-examples.git Fausto Spoto Universit` a di Verona, Italy - 1 / 45

Transcript of Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and...

Page 1: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Java and Android Concurrency

Concurrency and GUIs

[email protected]

[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

Page 2: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 3: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 4: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 5: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 6: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A First Example of the Use of Swing

Fausto Spoto Universita di Verona, Italy - 6 / 45

Page 7: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Opening a New Window in Swing

Fausto Spoto Universita di Verona, Italy - 7 / 45

Page 8: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Frame Implements a Window and Contains Components

Fausto Spoto Universita di Verona, Italy - 8 / 45

Page 9: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Component Knows how to Draw Itself

Fausto Spoto Universita di Verona, Italy - 9 / 45

Page 10: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Second Example of the Use of Swing

Fausto Spoto Universita di Verona, Italy - 10 / 45

Page 11: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Second Example of the Use of Swing

Fausto Spoto Universita di Verona, Italy - 11 / 45

Page 12: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Second Example of the Use of Swing

Fausto Spoto Universita di Verona, Italy - 12 / 45

Page 13: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Third Example of the Use of Swing: Library Components

Fausto Spoto Universita di Verona, Italy - 13 / 45

Page 14: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

TextFields, Labels, Container Panels. . .

Fausto Spoto Universita di Verona, Italy - 14 / 45

Page 15: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Text Areas, Scroll Bar Decorators. . .

Fausto Spoto Universita di Verona, Italy - 15 / 45

Page 16: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 17: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Listeners as Explicit Classes (Name Pollution)

Fausto Spoto Universita di Verona, Italy - 17 / 45

Page 18: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Listeners as Inner Classes (Still too Long)

Fausto Spoto Universita di Verona, Italy - 18 / 45

Page 19: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Listeners as Anonymous Inner Classes (OK-ish)

Fausto Spoto Universita di Verona, Italy - 19 / 45

Page 20: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Listeners as Lambdas (only Java 8) (great!)

Fausto Spoto Universita di Verona, Italy - 20 / 45

Page 21: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

Buttons

Fausto Spoto Universita di Verona, Italy - 21 / 45

Page 22: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Serious Swing Application!

Fausto Spoto Universita di Verona, Italy - 22 / 45

Page 23: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 24: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 25: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 26: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The Model-View-Controller Design Pattern

Fausto Spoto Universita di Verona, Italy - 26 / 45

Page 27: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 28: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The MVC Triple 1/2

Fausto Spoto Universita di Verona, Italy - 28 / 45

Page 29: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The MVC Triple 2/2

Fausto Spoto Universita di Verona, Italy - 29 / 45

Page 30: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The Starting Point of the Program

Fausto Spoto Universita di Verona, Italy - 30 / 45

Page 31: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The Model

Fausto Spoto Universita di Verona, Italy - 31 / 45

Page 32: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The Model: Implementation

Fausto Spoto Universita di Verona, Italy - 32 / 45

Page 33: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The Controller

Fausto Spoto Universita di Verona, Italy - 33 / 45

Page 34: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View

Fausto Spoto Universita di Verona, Italy - 34 / 45

Page 35: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View: First Implementation 1/4

Fausto Spoto Universita di Verona, Italy - 35 / 45

Page 36: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View: First Implementation 2/4

Fausto Spoto Universita di Verona, Italy - 36 / 45

Page 37: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View: First Implementation 3/4

Fausto Spoto Universita di Verona, Italy - 37 / 45

Page 38: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View: First Implementation 4/4

Fausto Spoto Universita di Verona, Italy - 38 / 45

Page 39: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

A Custom Component for Histograms

Fausto Spoto Universita di Verona, Italy - 39 / 45

Page 40: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View: Second Implementation 1/3

Fausto Spoto Universita di Verona, Italy - 40 / 45

Page 41: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View: Second Implementation 2/3

Fausto Spoto Universita di Verona, Italy - 41 / 45

Page 42: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

The View: Second Implementation 3/3

Fausto Spoto Universita di Verona, Italy - 42 / 45

Page 43: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 44: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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

Page 45: Concurrency and GUIs - Java and Android Concurrency · Java and Android Concurrency Concurrency and GUIs fausto.spoto@univr.it git@bitbucket ... void java.awt.EventQueue.invokeLater(Runnable

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